Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 - Devise With OmniAuth - Redirect after signing in goes to /users/sign_in

Im using devise with omniauth for signing in users with facebook. I want them to be redirected after signing in to the page they were on after before signing in.

I've used the

  def after_sign_in_path_for(resource_or_scope)
    store_location = session[:return_to]
    clear_stored_location
    (store_location.nil?) ? "/" : store_location.to_s
  end

in my application controller, and made a sessions_helper with this code

  def deny_access
    store_location
    redirect_to new_user_session_path
  end

  def anyone_signed_in?
    !current_user.nil?
  end

  private

    def store_location
      session[:return_to] = request.fullpath
    end

    def clear_stored_location
      session[:return_to] = nil
    end

And to fix the problem with being redirected to "services/" where I have the logic for the authentication with facebook and other platforms I've used the

skip_before_filter :store_location

in services and other controllers that I don't to be stored as locations.

Q1 The problem I'm having now is when I use ajax and render a login form in a modal window is that when a user successfully signs in it gets redirected to /users/sign_in/. I don't have a user controller and tried to make a sessions_controller.rb and added the skip_before... there but it doesn't work.

This is my routes for sign_in

new_user_session       GET   /users/sign_in(.:format)    {:action=>"new", :controller=>"devise/sessions"}
    user_session      POST   /users/sign_in(.:format)    {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session   GET   /users/sign_out(.:format)   {:action=>"destroy", :controller=>"devise/sessions"}

Q2 I've tried to use the redirect when users sign out

  def after_sign_out_path_for(resource_or_scope)
      (session[:return_to].nil?) ? "/" : session[:return_to].to_s
  end

But that only redirects me to the root page.

I really appreciate any help in this,

like image 774
Philip Avatar asked Jun 15 '11 08:06

Philip


3 Answers

I answered this here

Devise doesn't redirect properly to stored location when using omniauth provider like facebook

in application_controller.rb

 def after_sign_in_path_for(resource_or_scope)
   if request.env['omniauth.origin']
      request.env['omniauth.origin']
    end
end
like image 135
Daxon Avatar answered Nov 16 '22 09:11

Daxon


did you consider the Devise provided helper sign_in_and_redirect(resource_or_scope, *args) (details can be found here)? You also get sign_out_and_redirect(resource_or_scope) amongst others.

Q2. do try inspecting session[:return_to] - it's most likely nil.

like image 44
Michael De Silva Avatar answered Nov 16 '22 09:11

Michael De Silva


You can pass a parameter like: /auth/facebook?referrer=/path/to/redirect

After the Facebook authentication this parameter will be available to you in env["omniauth.params"].

like image 1
dobrinov Avatar answered Nov 16 '22 09:11

dobrinov