Ruby on Rails Develop code after login

I have a RoR application using Devise to login. There is some code that executes when creating a new user record, placing it in the user.rb file as a call to after_create / macro / whatever. I need this code to run after each login, and not run when a new user is created.

With some Googling, it seems like one option is to place the Warden callbacks in the devise.rb code. My questions:

  • Is this correct and / or is there a better way to do this?
  • If this is the right approach ...
    • Should the Warden :: Manager ... defs method go to devise.rb inside or after Devise.setup?
    • Is after_authentication a callback I should use? I just check if the directory exists based on the username, and if not, create it.
+7
ruby-on-rails devise warden
source share
3 answers

Just a subclass. Create a session controller and place your usual behavior there:

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

And then create your controller as follows:

 # 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 
+18
source share

I found that using the Devise Warden hook is allowed after capturing login events by looking for the event:: set_user :.

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 
+4
source share

I think this is a duplicate question. Yes, you can execute the code after each successful login. You can write the code in your ApplicationController. Also see http://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in . Also, check how to redirect to a specific page upon successful registration using devise gem rails? for some ideas.

You can do something like:

 def after_sign_in_path_for(resource_or_scope) Your Code Here end 

Link Can I perform user actions after successfully logging in using the development program?

You can also inherit from developing a session class and use after_filter to log in.

0
source share

All Articles