Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a session variable in devise on sign in

I'd like to set a session variable once a user signs in based on a certain field in the User table. I don't want to have to create a custom Devise controller if I don't have to. Is there a way? Or will I have to go the custom controller route?

like image 891
Patm Avatar asked Sep 14 '11 20:09

Patm


2 Answers

There is a callback after_sign_in_path_for, you can add it in your ApplicationController

protected

def after_sign_in_path_for(resource)
  session[:domain_prefix] = current_user.domain_prefix
  user_path(resource)
end

Dont forget return the path in the last line of method, otherwise the callback will redirect the request to content of session[:domain_prefix]

like image 57
Shairon Toledo Avatar answered Oct 22 '22 10:10

Shairon Toledo


How about this one:

The first resource I'd look at is http://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in. Also, check out How to redirect to a specific page on successful sign up using rails devise gem? for some ideas.

You can do something like:

def after_sign_in_path_for(resource_or_scope)
  session[:account_type] = current_user.account_type

end

You can implement this method in your ApplicationController or in a custom RegistrationsController.

like image 6
Mohit Jain Avatar answered Oct 22 '22 09:10

Mohit Jain