Can I execute custom actions after successful sign in with Devise?
Edit: Please consider that this was once a good solution, but there are probably better ways of handling this. I am only leaving it here to give people another option and to preserve history, please do not downvote.
Yes, you can do this. The first resource I'd look at is 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)
session[:my_account] = current_user.account
profile_url
end
You can implement this method in your ApplicationController or in a custom RegistrationsController.
Devise redirect after sign_in and perfom POST action
Found the solution !
There is actually a gem called repost
, that helps you redirect to a POST action. I implemented it in my solution.
Here is the whole technique I applied. To redirect after sign-in and then complete the POST action when the login was successful:
First, I create a session session[:previous_url]
for later on to be used to redirect back here.
def show
session[:previous_url] = request.fullpath #url for user to come back after signing in
...
attendee_controller.rb : we create session[:attendee]
def create
if !current_user
session[:attendee] = true
redirect_to user_session_path
else
...
app\controllers\users\sessions_controller.rb (user_session_path)
protected
def after_sign_in_path_for(resource_or_scope)
if session[:attendee]
previous_url = session[:previous_url]
session[:previous_url] = nil #clear session
previous_url #going back to event page
else
super
end
end
More info into after_sign_in_path_for
method here in the Devise How-To Wiki.
previous_url
will bring us back to show
view from the events.
Here is where the gem will come save the day :
app\controllers\events_controller.rb
before_action :attendee?, only: :show
private
def attendee?
if session[:attendee]
session[:attendee] = nil
repost(attendee_event_path, options: {authenticity_token: :auto})
end
end
This is the gem template :
repost('url' , params: {}, options: {})
Here is the gem link: https://github.com/vergilet/repost
Devise: after successful login hook
http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/DatabaseAuthenticatable#after_database_authentication-instance_method
In the user model where you're using devise create a after_database_authentication instance method.
How to redirect to a specific page on successful sign up using rails devise gem?
This page is for you: http://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in
Rails: Devise: redirect after sign in by role
By default Devise does route to root after it's actions. There is a nice article about overriding these actions on the Devise Wiki, https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in
Or you can go even farther by setting stored_locations_for(resource)
to nil, and then have different redirects for each action, ie: after_sign_up_path(resource)
, after_sign_in_path(resource)
and so on.
Devise - How to trigger a sign_in on page user already has access to
This sounds like you need some controller specific code.
When a user clicks on the link, have the corresponding action do something like this:
def image_action # Whatever the action name is
if current_user # current user is given to you by devise
# Do stuff that a logged in user would do
else
# redirect to login page
end
end
The idea being that you leave the logic out of the view, and instead either have the controller serve the logged in user the appropriate data / page or have an unauthenticated user redirected to the login page.
current_user
is a method given to you by devise that evaluates either to the currently logged in user making the request or nil
if the user is not logged in.
If you post your controller code, I'll file out more of my example code, I just didn't want to make it too specific at risk of being confusing.
Related Topics
Rails Form Validation Conditional Bypass
Loaderror Running Mongrel with Rails3 and Ruby 1.9.2
How to Make Httparty Ignore Ssl
Rails 3 Install Error: "Invalid Value for @Cert_Chain"
Problem with Quantifiers and Look-Behind
Howto Rank Items by Balance in Ruby on Rails
Why Does Array.Each Behavior Depend on Array.New Syntax
Selenium-Webdriver Ruby --> How to Wait for Images to Be Fully Loaded After Click
When to Use Nested Classes and Classes Nested in Modules
Ruby Inject with Initial Being a Hash
How to Ftp in Ruby Without First Saving the Text File
Rails - Local Variables Versus Instance Variables
Missing Host to Link To! Please Provide the :Host Parameter, for Rails 4
Why Is Gets Throwing an Error When Arguments Are Passed to My Ruby Script
Difference Between Downcase and Downcase! in Ruby
How to Track the Execution Process of Ruby Program
How to Configure Ruby to Enter the Debugger on Ctrl-C (Sigint)