Search code examples
ruby-on-rails-3deviseomniauthfacebook-authentication

Devise + Facebook Omniauth Error on redirect


I am using devise, omniauth & facebook-omniauth for my Rails 3.1 app. After authentication I wanted to redirect the user to the page was viewing. I have used the following code for the same:

def facebook
@user = Spree::User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
if @user.persisted?
flash[:notice] = "Yipee! You were successfully authorized from your Facebook account!!"
sign_in @user, :event => :authentication
redirect_to request.referrer
end

This gives me the following error only at the time of user creation:

ActionController::ActionControllerError in Spree::OmniauthCallbacksController#facebook
Cannot redirect to nil!

The following times when the user has already been created, no errors are shown during & after log in.

How do you suggest I fix this? Thanks!


Solution

  • you can overwrite the functions for sign in/ sign up path in your application controller:

    def after_sign_up_path_for(resource)
      credit_path 
      return request.env['omniauth.origin'] || session[:return_to] 
    end 
    
    def after_sign_in_path_for(resource)
      return request.env['omniauth.origin'] || session[:return_to]
    end
    

    use sessions to store the current path in the path that you want them to go to: session[:return_to] = request.url #store current location

    or you create a method that will always be called once they go to a path and store that location. watch out for a giant loop redirection when you do that though.