urjit on rails
urjit on rails

Reputation: 1893

devise+omniauth devise helper like current_user,user_signed_in? not working

I am using devise and create login with Facebook using omniauth, but having problem of lost the devise helper methods access like current_user and user_signed_in? methods are not working.

EDIT

AuthenticationController

def create
    omniauth = request.env["omniauth.auth"]    
    user = User.find_by_provider_and_uid(omniauth["provider"], omniauth["uid"]) ||       User.create_with_omniauth(omniauth)    
    session[:user_id] = user.id    
    redirect_to dashboard_path(user.id), :notice => "Signed in!"    
end  

redirect_to USercontroller dashboard method

UserController

before_filter  :logged_in

 def dashboard    
    @user = User.find(params[:id])   
    @comment = Comment.new    
    @comments = @user.comments.all.paginate(:page => params[:page], :per_page => 5)    
 end 

so here control should go to dashboard method after checking logged_in method in ApplicationController

logged_in method in ApplicationController

Application Controller

def logged_in    
    if user_signed_in?     
       return true    
    else    
       redirect_to root_path    
       flash[:message] = "please login"    
    end     
  end 

when I logged in using facebook following code generated at console

Started GET "/users/52/dashboard" for 127.0.0.1 at Thu Mar 29 12:51:55 +0530 2012     
Processing by UsersController#dashboard as HTML     
  Parameters: {"id"=>"52"}     
Redirected to http://localhost:3000/     
Filter chain halted as :logged_in rendered or redirected     
Completed 302 Found in 2ms (ActiveRecord: 0.0ms)     

in the above code control is render from logged_in method to root_path but it shold render dashboard_path

So I am guessing User_signed_in? helper is not working I also use current_user in stead of that generate same error

Upvotes: 0

Views: 2971

Answers (2)

Robin
Robin

Reputation: 21884

After creating the user account via Facebook, how do you sign in the user?

You should still be using devise helpers like sign_in_and_redirect. Something like:

user = User.build_from_omniauth(omniauth)
if user.save
  sign_in_and_redirect(:user, user)
end

Then you should be able to use helpers like current_user and user_signed_in? (which just check if current_user is not nil).


Taking a look at your edit, my answer is still valid. What you need to do is use sign_in_and_redirect(:user, user) instead of just setting the id in the session. You can easily customize where the user is redirected after sign in with devise.

Another thing, remove this logged_in filter, Devise has a authenticate_user! method that you can use as a before_filter. It will redirect the user to the sign in page, and when they login, it will redirect them to the page they were trying to access.

You're using Devise, so try to take advantage of that, and go read the doc ;)

Upvotes: 0

alony
alony

Reputation: 10823

As I see, user_signed_in? is working, but returns false, as for Devise user is not logged in. To fix this, just replace the session id storing with Devise sign_in method in your controller action:

def create
    omniauth = request.env["omniauth.auth"]    
    user = User.find_by_provider_and_uid(omniauth["provider"], omniauth["uid"]) ||       User.create_with_omniauth(omniauth)    
    sign_in(:user, user)

    # actually if you really really need that id in the session, you can leave this line too :)
    session[:user_id] = user.id 

    redirect_to dashboard_path(user.id), :notice => "Signed in!"    
end 

Upvotes: 7

Related Questions