I would like to execute an action after the user has logged in. I need something like:
after_filter :log_login
The method should be called as soon as the user has logged in.
You could try inheriting the sessions controller, and add more of your custom login proc. http://github.com/zmbmartin/devise-roles-user-management. I use this as an example, it inherit registration controller, not example the same, but the principle is the same.
As already answered here:
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 put that code into an initializer (eg. config/initializers/warden_callback.rb)
Related
I was wondering if anyone knows a simple way to make devise 'create' an account if it doesnt exist at login, allowing me to use a single page.
Yes, this is possible. Devise's sessions#create method yields to a block - see https://github.com/plataformatec/devise/blob/master/app%2Fcontrollers%2Fdevise%2Fsessions_controller.rb#L20
So, you'd need to override that controller's create method, and use the yield to add a new account if the user is not successfully signed in.
Edit: here's a quick pseudo-code example of what your overriden create method should look like:
def create
super do |user|
return if current_user # successful login
# Here, if the user doesn't exist,
# create a new record and log them in.
end
end
You need to custom your controller and put the 'create' action in it.
Here's the guide for customizing your controller: https://github.com/plataformatec/devise#configuring-controllers
After a user log in, I want to manually log this event and increment a counter column in database.
Is there something like after_login in Devise? Just as ActiveRecord's before_save?
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)
How do I execute a particular function after a user has signed up.
(I wanted to add to one of my associations, I already have it coded in a non-devise rails but now I need it here)
Device has provided helper action 'after_sign_in_path_for' you can override it within your application controller.
def after_sign_in_path_for(resource_or_scope)
.... #write your customize code or call any method
end
For sign up it would look like:
def after_sign_up_path_for(resource_or_scope)
if resource_or_scope.is_a? User # and perhaps other conditions
#... do something, go somewhere
else
super
end
end
Ofc. Assuming that your Devise user model is called User.
You can use the after_create callback in your User model.
The following guide has tons of examples: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
I'm relatively new to rails. I have Devise set up, and want to run some callback code after users sign in.
Looking at the Warden wiki page, I can use the "after_set_user" callback to perform this logic, for example:
Warden::Manager.after_set_user do |user, auth, opts|
unless user.active?
auth.logout
throw(:warden, :message => "User not active")
end
end
However, I'm not sure where I should be storing this stuff. My first thought is that I could put it in config/initializers/devise.rb. Is that correct? It doesn't feel right putting what is essentially controller code in the config directory.
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
I am using Devise, and when someone logs in I would like to execute a custom method.
Similarly to how you use before_save to execute a method before the account/model is updated/saved, or before_create to do the same before the object is initially created.
I would like to do the same, but for users logging in.
How do I do that ?
In your application_controller.rb add the following code:
def after_sign_in_path_for(resource)
execute_custom_function()
super
end
You can utilize Devise's Controller Helper.