Devise Remember User Email

Posted By Weston Ganger

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:

Article Topic:Software Development - Ruby / Rails

Date:June 27, 2015