Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Always getting 401 Unauthorized with new install of Rails + Devise

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=>'[email protected]', :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:

Started POST "/admin/user/sign_in" for 127.0.0.1 at 2012-12-22 13:10:56 -0500
Processing by Admin::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"wYLsalxN9rTv8P8bvYuT0wZcvlFbu6b1SvoCyKtTCII=", "admin_user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1
(0.1ms)  begin transaction
(0.0ms)  commit transaction
Completed 401 Unauthorized in 69ms

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?

like image 203
koosa Avatar asked Dec 22 '12 18:12

koosa


People also ask

Does devise work with rails 7?

Our out-of-the box Devise setup is now working with Rails 7. Once again, if you'd like to refer to any of the code for this setup, or use the template wholesale for a new app, the code is available on GitHub, and you may also use it as a template repo to kick off your own Rails 7 devise projects.

What is 401 error in REST API?

For the Fuel/REST API, access tokens expire one hour after they are issued, when you use a legacy package. If you attempt to use an expired token, you'll receive a "401 Unauthorized HTTP" response. When this happens, you'll need to refresh the access token.

What does HTTP Error 401?

The HyperText Transfer Protocol (HTTP) 401 Unauthorized response status code indicates that the client request has not been completed because it lacks valid authentication credentials for the requested resource.


2 Answers

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.

like image 85
koosa Avatar answered Sep 27 '22 15:09

koosa


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

like image 31
stereodenis Avatar answered Sep 27 '22 17:09

stereodenis