Search code examples
ruby-on-rails-4devisewarden

Adding data to a Devise session


I'm using Rails 4 and would like to add more data to the Session that Devise/Warden creates when logging in.

The user_id is stored in ["warden.user.user.key"], but i'd like to add the user's email and username as well.

This is related, but the opposite of what I'd like to do: How to access session from Warden/Devise after_authentication callback in Rails


Solution

  • I figured it out by adding this to my config/initializers/devise.rb file. Not sure if this is safe or the right way to go about it, but it works.

    Warden::Manager.after_authentication do |user,auth,opts|
      auth.raw_session['warden.user.user.email'] = user.email
      auth.raw_session['warden.user.user.username'] = user.username
    end