Devise signs in, but does not save current user

251 views
Skip to first unread message

David Dawson

unread,
May 20, 2014, 9:54:45 PM5/20/14
to plataforma...@googlegroups.com
Hi, I'm a new user, testing out devise, and I'm having some troubles getting things to work. So far, I've been able to create a user in a user table in a couple of different ways (twitter and email), and I've been able to see that Devise thinks I'm logged in. But I have no access to a current_user, and my user_signed_in? returns false.

When I try to sign in, I see something like this appear in the logs (the user from my users table that I log in with is found by matching a twitter uid in the UserType1 table, then finding the associated user in the User table): 
UserType1 Load (0.1ms)  SELECT  "user_type1s".* FROM "user_type1s"  WHERE "user_type1s"."twitter_uid" = ########## LIMIT 1
  User Load (0.2ms)  SELECT  "users".* FROM "users"  WHERE "users"."type_id" = ? AND "users"."type_type" = ? LIMIT 1  [["type_id", 2], ["type_type", "UserType1"]]
  User Load (0.1ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 2  ORDER BY "users"."id" ASC LIMIT 1
Redirected to http://localhost:3000/
Completed 302 Found in 8ms (ActiveRecord: 0.8ms)

And before the root page loads, I get something like this:
  User Load (0.2ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 2  ORDER BY "users"."id" ASC LIMIT 1

If I sign up more users, I get the user id number incrementing correctly, and when I look at the table, I'm correctly seeing the "sign in count" correctly incrementing both between when I first sign up and log in (going from 0 to 1) and when my twitter user logs in again after (going from 1 to 2, or however high I want it to go). If I try to use the /users/sign_in path, I get a flash saying that I'm already logged in.  If I run that select query in a rails console, I get the correct user back.

I've been looking around, and I've seen some answers that don't appear to work for me: I've completely turned off CSRF protection for now (removed the line from my application controller) and my devise_parameter_sanitizer for sign_in is set to permit everything in my user controller.

I also see this at the bottom of my session after I've signed in:
{"session_id"=>"753cb906d43b2636ae990840687bca44", "_csrf_token"=>"Z9+N8qeTvQRFMdKrn78sR+ZLDQXpnyg2yJV86j1bqTM=",  
"oauth"=>{"twitter"=>{"callback_confirmed"=>true}}, "warden.u...@fake.com.key"=>"te...@fake.com", "warden.user.user.key"=>[[2], 
"$2a$10$.TZPi/kmREve96ymRc2mOe"], "flash"=>{"discard"=>["alert"], "flashes"=>{"alert"=>"You are already signed in."}}}}, 
@delegate={"session_id"=>"753cb906d43b2636ae990840687bca44", "_csrf_token"=>"Z9+N8qeTvQRFMdKrn78sR+ZLDQXpnyg2yJV86j1bqTM=",  
"oauth"=>{"twitter"=>{"callback_confirmed"=>true}}, "warden.u...@fake.com.key"=>"te...@fake.com",  
"warden.user.user.key"=>[[2], "$2a$10$.TZPi/kmREve96ymRc2mOe"], "flash"=>{"discard"=>["alert"],  
"flashes"=>{"alert"=>"You are already signed in."}}}, @loaded=true, @exists=true>

I'm not exactly sure how to read that, but it seems like Warden knows the user's id, what the user's email is and how the authentication was done.

What could be causing my setup to not actually set anything when I do an "authenticate_user!", when it knows who the user is supposed to be and can find it? Is there any additional logging I can do to see where things are going wrong?

David Dawson

unread,
May 22, 2014, 2:51:44 AM5/22/14
to plataforma...@googlegroups.com
After doing some more testing, I think my user helper isn't being generated properly. I took a look at the source for creating the helpers (https://github.com/plataformatec/devise/blob/master/lib/devise/controllers/helpers.rb) and I added these things to the controller where I have my before filter:

def user_signed_in?
!!current_user
end

def current_user
@current_user ||= warden.authenticate(scope: => :user)
end

def user_session
current_user && warden.session(:user)
end

When I check the value of the current user, I get the correct user information (I can request id, email, etc. and the response is correct) and user_signed_in? evaluates to true.

When in a rails console, I've checked:
>> Devise.mappings.keys
=> [:user]


I don't know what else could be going wrong - using exactly the code that should be generated gives me all the right values. At the same time, something is being generated with those names, because there's no "no method" errors being thrown, it's just that the thing being generated must be wrong.

Any ideas how to debug this?
Reply all
Reply to author
Forward
0 new messages