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

Philip picture Philip · Jun 15, 2011 · Viewed 8.4k times · Source

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,

Answer

Daxon picture Daxon · Sep 22, 2011

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