Always getting 401 Unauthorized with new install of Rails + Devise

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

I have a new install of Rails and am trying to set up authentication with Devise. As far as I can tell I have a very basic set up that should work, but whenever I try to log in with the default Devise sign in form I get an Unauthorized error. I am sure my credentials are correct as I created a User to test with in the console like so:

User.new({:email=>'mark@markdavies.com.au', :priv_level => 'admin', :password=>'mypassword', :password_confirmation=>'mypassword'}).save

My User model:

class User < ActiveRecord::Base    # Include default devise modules. Others available are:   # :token_authenticatable, :confirmable,   # :lockable, :timeoutable and :omniauthable   devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :confirmable    # Setup accessible (or protected) attributes for your model   attr_accessible :email, :password, :password_confirmation, :remember_me, :priv_level, :unconfirmed_email   # attr_accessible :title, :body    has_one :supplier  end

My log:

Is there any way I can get more information about what is failing from Devise? When I create the user in the console is the encryption used different than through the forms?

回答1:

Well this little exercise in frustration turned out to be a good lesson in RTFM. I had set up Devise with confirmable, and when I created my layouts I neglected to insert the following lines:

<p class="notice"><%= notice %></p> <p class="alert"><%= alert %></p>

... as it clearly states to do in the getting started guide. When I inserted these I got the error message "You need to confirm your email address before logging in."

In the console I set confirmed_at = Time.now for the User, and voilà, I can now log in.



回答2:

In my situation it causes by this

before(fails with 401)

resources :users devise_for :users

after(success)

devise_for :users resources :users

so, try to make right order in routes.rb



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!