Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I execute custom actions after successful sign in with Devise?

I have an app that has basic Devise authentication. After sign in, I would like to look up the user account (user belongs_to account, account has_many users), and store that in the session so that it is available like the @current_user.

What is the rails way of storing session in formation like this? Is there a hook I can use with Devise to execute code after successful sign-in?

like image 777
vfilby Avatar asked Jan 20 '11 23:01

vfilby


3 Answers

Actually, the accepted answer does not work properly in case of combined Omniauth and Database login modules in Devise.

The native hook that is executed after every successfull sign in action in Devise (disregarding the user authentication channel) is warden.set_user (called by devise sign_in helper: http://www.rubydoc.info/github/plataformatec/devise/Devise/Controllers/SignInOut#sign_in-instance_method).

In order to execute custom action after successfull user sign in (according to Warden Docs: https://github.com/hassox/warden/wiki/Callbacks), put this into initializer (eg. after_sign_in.rb in config/initializers)

Warden::Manager.after_set_user except: :fetch do |user, auth, opts|
  #your custom code
end

Update 2015-04-30: Thanks to @seanlinsley suggestion (see comments below), I have corrected the answer to include except: :fetch in order to trigger the callback only when user is authenticated and not every time it is set.

Update 2018-12-27 Thanks to @thesecretmaster for pointing out that Warden now has built-in callbacks for executing your own code on after_authentication https://github.com/wardencommunity/warden/wiki/Callbacks#after_authentication

like image 158
MatFiz Avatar answered Nov 09 '22 23:11

MatFiz


Edit: Please consider that this was once a good solution, but there are probably better ways of handling this. I am only leaving it here to give people another option and to preserve history, please do not downvote.

Yes, you can do this. 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[:my_account] = current_user.account
  profile_url
end

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

like image 18
Anthony Panozzo Avatar answered Nov 10 '22 00:11

Anthony Panozzo


i'm using rails 5 and devise 4.2.1, my solution is overide devise function on user model:

def after_database_authentication
    # here's the custom code
end

and the user model will look like this:

class User < ApplicationRecord
    devise :database_authenticatable, :registerable,
        :recoverable, :rememberable, :trackable, :validatable,
        :timeoutable, :lockable
    def after_database_authentication
        # here's the custom code
    end
end

it was called just after the authentication, i read it from this devise documentation, hope this could help

like image 10
Esgi Dendyanri Avatar answered Nov 10 '22 00:11

Esgi Dendyanri