Rails

Devise Remember User Email

Posted by Weston Ganger on June 27, 2015

I thought that when you selected remember me it would autofill the email field. Turns out that’s not the intention of that.

So heres how to remember the user email if remember me is checked. You can change this to save any data you want in a cookie after sign in.

Add this at the end of your config/initializers/devise.rb or in another initializer file.


# if this is in config/initializers/devise.rb make sure its outside of the Devise block
Warden::Manager.after_authentication do |user, auth, opts|
  if user.remember_me
    auth.cookies[:email] = {value: user.email, expires: 2.weeks.from_now}
  else
    auth.cookies.delete :email
  end
end

Now it will store the users email in a cookie if they selected remember_me and if not it will attempt to delete the cookie.


Related External Links:


Posted in Rails and Tagged with rails devise cookies 


Want me to help develop your next project or application?

Contact Us

Specializing in Ruby-on-Rails, Javascript, Multi-Platform Electron Desktop Apps and Hybrid Cordova Mobile Apps.

Recommended Posts