开发者

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 u开发者_高级运维sers), 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?


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


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.


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


I resolved this problem by overriding the create method of the session controller like following

class Admin::SessionsController < Devise::SessionsController

   def create 
     super
     # here goes my code
     # my settings, etc 
     # do something with current_admin.fullname, for example
  end 
end

In other words, if authentication is successful (by calling super) then I perform my settings.


In application controller, you can simply add an after action.

app/controllers/users/application_controller.rb

class ApplicationController < ActionController::Base
  after_action :do_something

  def do_something
    # do something
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜