Ruby on Rails Devise code after login

后端 未结 3 1167
甜味超标
甜味超标 2020-12-16 01:32

I have an RoR app using Devise for logins. There is some code that is executed when a new User record is created, by being put in the user.rb file as an after_create call/m

相关标签:
3条回答
  • 2020-12-16 01:47

    I found that using the Devise Warden hook cleanly allowed after login event trapping by looking for the ":set_user" :event.

    In user.rb:

    class User < ApplicationRecord
      Warden::Manager.after_set_user do |user, auth, opts|
        if (opts[:scope] == :user && opts[:event] == :set_user)
          # < Do your after login work here >
        end
      end
    end
    
    0 讨论(0)
  • 2020-12-16 01:50

    Just subclass Devise's sessions controller and put your custom behaviour there:

    # config/routes.rb
    devise_for :users, :controllers => { :sessions => "custom_sessions" }
    

    And then create your controller like this:

    # app/controllers/custom_sessions_controller.rb
    class CustomSessionsController < Devise::SessionsController
      before_filter :before_login, :only => :create
      after_filter :after_login, :only => :create
    
      def before_login
      end
    
      def after_login
      end
    end
    
    0 讨论(0)
  • 2020-12-16 01:59

    I think this is an duplicate question. Yes you can execute code after every successful log in. you could write the code in your ApplicationController. Also have a look at 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)
    Your Code Here
    end
    

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

    You could also inherit from devise session's class and use after_filter for logins.

    0 讨论(0)
提交回复
热议问题