开发者

When doing a manual sign_in for the user in Devise, how can you set remember me to yes?

开发者 https://www.devze.com 2023-01-23 23:24 出处:网络
In Devise, I\'m signing in my user like this: sign_in_and_redirect(:user, user) In the default sign in page, there\'s a checkbox 开发者_JAVA百科that the user can select so that they don\'t have to

In Devise, I'm signing in my user like this:

sign_in_and_redirect(:user, user)

In the default sign in page, there's a checkbox 开发者_JAVA百科that the user can select so that they don't have to sign in again when they return to the site. But when you do the sign in with the sign_in_and_redirect(:user, user) line, I can't work out how to set that parameter to yes. Does anyone know how? Thanks for reading.


current_user.remember_me!

https://github.com/plataformatec/devise/blob/master/lib/devise/models/rememberable.rb#L54

Note that this only updates the remember_created_at value of the user record. But for this to work correctly, a validation token also needs to be stored in the Devise cookie. To achieve both these things, follow Dmytrii's advise and user the remember_me <USER> controller method instead:

include Devise::Controllers::Rememberable
...
remember_me <USER_OBJECT>


Did some testing. Presenting the findings for others.

The simplest solution, assuming the user object has the rememberable module defined on the devise declarable, is to set remember_me to true on the user before sign in and redirect:

@user.remember_me = true
sign_in_and_redirect(@user, :event => :authentication)
0

精彩评论

暂无评论...
验证码 换一张
取 消