Devise Call Backs

Does devise have 'callback'?

Devise uses Warden behind the scenes and Warden supplies you with a number of callbacks:

https://github.com/hassox/warden/wiki/callbacks

Have a look at the after_authentication callback. That's what you are looking for.

Code:

Warden::Manager.after_authentication do |user, auth, opts|
# your code here..
end

You can simply create a new initializer file and put the code there. (Like /config/initializers/warden_callbacks.rb)

Devise call backs

I'm no expert but I believe the callbacks (hooks) are at the Warden level (Devise is built on top of Warden).

after_set_user and before_logout in Warden should do the trick for you but there are other options listed in Warden::Hooks

Adding custom callback to user model with devise

If you want to customize the UserController you can easily do that by

rails generate devise:controllers [scope]

For example

rails generate devise:controllers users

Check out the documentation here

How can I use Devise methods in my ConnectionAdapter callback?

You can't use the checkout callback, at the point that it's executed, it has no connection to the controller context. The fact that you've defined it here in your ApplicationController is irrelevant to the context it's actually executed in.

You will need to set the connection option in the before_action so you're running in the controller context. Something like:

before_action :set_user_context

def set_user_context
if current_user
ApplicationRecord.connection.execute "DBMS_SESSION.SET_CONTEXT('whatever', 'goes', 'here', '#{current_user.username}')"
end
end

...or something like that. Note that you might want to add a checkin callback to clear the value when the connection is finished with.

Btw, I answered a nearly identical question a few days ago: https://stackoverflow.com/a/54837596/152786 Different commands though, but might help.

How do I add callbacks when using the devise-guests gem?

Found an example controller here

In your ApplicationController you need to define and then set the callback like so:

define_callbacks :logging_in_user
set_callback :logging_in_user, :before, :transfer_guest_user_actions_to_current_user

def transfer_guest_user_actions_to_current_user
# todo
end

Where should warden callbacks be placed in a rails app?

Warden hooks need to be required when your application is booting, so inside Devise's initializer at config/initializers/devise.rb is a good candidate.

However, the behavior you want to achieve will be better accomplished by using this Devise feature:

https://github.com/plataformatec/devise/wiki/How-To:-Customize-user-account-status-validation-when-logging-in

Rails 5 with Devise - how to create message of the day upon login?

So with devise you can just override the following controller

class UserSessionsController < Devise::SessionsController

after_action :after_login, :only => :create
def after_login
flash[:message_of_the_day] = "Welcome to our site!" #or make some db query to grab your saved text.
end
end

Then in your view, you can display the flash message like so:

<h1><%= flash[:message_of_the_day] %></h1> 

Add this to your routes file:

devise_for :users, :controllers => { :sessions => "user_sessions" }

When you create a new user you will need to create a user_registrations_controller.rb file and place the following code in that file.

class UserRegistrationsController < Devise::RegistrationsController

after_action :after_sign_up, :only => :create

def after_sign_up
if current_user.present?
flash[:message_of_the_day] = "Welcome to our site"
end
end
end

Then edit the route to look like this:

devise_for :users, :controllers => { :sessions => "user_sessions", registrations: 'user_registrations' }


Related Topics



Leave a reply



Submit