Rails reinvent the hook on_login

I am looking to connect to a device after logging in after a session. How can i do this?

Basically, I want to set the user's location every time they log in, and for this I will need sorting after login.

+4
source share
3 answers

Priority updates the timestamp value of user.current_sign_in_at upon successful login. That way, you can simply add the before_save filter to your user model. In this filter, check whether the value of this field has changed, and if it is set, specify the location of the user.

BTW - I'm not sure what you mean by "location" - if you mean the IP address, Devise already saves this for you.

+4
source

The developer is built on Warden, so you can use the Warden after_authentication hook.

Put this in the initializer:

 Warden::Manager.after_authentication do |user,auth,opts| # do something with user end 

The remote IP address and other request information is stored in auth.request (i.e. auth.request.remote_ip).

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

+8
source

Here is a page from the developer wiki: How to: Redirect to a specific page with a successful login .

In general, the recommendation is to add the following method to the application controller:

application / controllers / application_controller.rb

 def after_sign_in_path_for(resource) custom_location_for(resource) || welcome_path end 

In the above code, resource means the object (user, account, etc.) for which you have implemented authentication. (An object that has devise_for in your routes.)

0
source

Source: https://habr.com/ru/post/1379185/


All Articles