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 


Need help on your next project or application?

I specialize in Ruby-on-Rails, AngularJS, Javascript, Bootstrap, and Hybrid Mobile Apps with Cordova & Ionic.

Contact Me

Recommended Posts