Using Devise's remember me functionality with a sign in through token authentication

2,123 views
Skip to first unread message

Robert Merrill

unread,
May 4, 2011, 12:04:30 PM5/4/11
to Devise
I am using Devise on my app and have set it so that I can use a URL
parameter, auth_key, to login to the site. It is working fine but I
would also like to have the user remembered. I tried using the
following:

current_user.remember_me!

That sets the remember in the database but no cookie is set on the
browser. If I manually set the checkbox when using the sign in page
to "remember me" it does indeed set the cookie and the remember me
functionality works. How can I get this working from a sign in
through token authentication? What am I doing wrong?

Thanks for any suggestions,
Robert

José Valim

unread,
May 4, 2011, 12:07:27 PM5/4/11
to Devise
Try this:

current_user.remember_me = true
sign_in current_user

Notice remember_me=true needs to be called before you sign in.

Robert Merrill

unread,
May 4, 2011, 12:38:20 PM5/4/11
to Devise
Thanks for the quick response José. I tried your recommendation but I
think I must be calling it after the user is already signed in so
nothing is happening and remember_created_at is nil after using the
auth_token to sign_in.

class NewslettersController < ApplicationController
before_filter :authenticate_user!
before_filter :sign_in_remember

def sign_in_remember
current_user.remember_me = true
sign_in current_user
end
end

I still need to call authenticate_user! first though so that
current_user exists, correct?

Sorry if this is a silly question. This is my first time using
Devise. Thanks so much for helping!

Robert Merrill

unread,
May 4, 2011, 11:56:14 PM5/4/11
to Devise
So I spent a while trying to get this working and I think I have it
working as I wanted but I am not sure this is the best way to do it.
I still have a sign_in_remember function but I added some lines to
make it sign out and then sign back in, which makes it create the
cookie as expected.

def sign_in_remember
@user = current_user
sign_out current_user
@user.remember_me = true
sign_in @user
end

That's what is working for me.
Reply all
Reply to author
Forward
0 new messages