Francois
Francois

Reputation: 10651

Rails 3 Devise Session Has value but <% if user_signed_in? %> is false

I hope you can help, have been struggling with this one for a while now :| Im using devise and Rails 3

When I click on Sign In, I get the devise Sign In page, I punch in the username and pw and click login.

After I clicked login I get redirected to the main page but now I still see the Sign In link, wich means that <% if user_signed_in? %> is still false. But it seems that there is some values in the session variable after I logged in, what is going on here? I have been using devise for some time and havent had any problems before. Thank guys!


Before I logged in with devise

<%= session %>
no value

<% if user_signed_in? %>
**is false**

After I logged in with devise

<%= session %>
_csrf_tokenAMUwVLu6G6rWfKICB43PYApFsYFRjVyJDSc2oU88uEk=warden.user.user.keyUser342$2a$10$.zslfggeUqvq.m/5LNSolOsession_id0db80c26bc36a4c1c74c223655dcb092

<% if user_signed_in? %>
**is false**

EDIT:

my routes.rb file

Cybercellar3::Application.routes.draw do
  devise_for :users

  get "home/index"


<% if signed_in? %>
**is still false**

EDIT2:

user.rb

class User < ActiveRecord::Base
      # Include default devise modules. Others available are:
      # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable

      # Setup accessible (or protected) attributes for your model
      attr_accessible :email, :password, :password_confirmation, :remember_me
    end

I think I found my problem in one of my controllers

application_controller.rb

    #facebook stuff
    def current_user
        @current_user ||= User.find_by_id(session[:user_id])
    end

    def signed_in?
        !!current_user
    end

    helper_method :current_user, :signed_in?

    def current_user=(user)
        @current_user = user
        session[:user_id] = user.id
    end
    #facebook stuff

after I removed this block it seemed to work perfectly :)

Upvotes: 5

Views: 3238

Answers (4)

Casey Bennington
Casey Bennington

Reputation: 1

As @Irina Nazarova said. Check to ensure that you don't have any methods my the same name you wrote yourself included in your project. It would seem Devise is using that first. This is what was causing my issue.

Upvotes: 0

Irina Nazarova
Irina Nazarova

Reputation: 1

Folks, I have been struggling with the same issue and finally found a solution! It might not be your case, but still.

My problem occurred when I was switching from my simple hand-written authentication process to Devise. And the reason why current_user or user_signed_in? were all blank and false upon signing in was that I had similarly named methods (sign_in, current_user, etc) in my sessions_helper.rb file that remained from the old system, and these (old) methods were called from a brand new Devise controller instead of the right ones from the Devise lib.

So I just commented out all the methods in my old sessions helper that serve authentication process. And then it worked!

Upvotes: 0

steve
steve

Reputation: 3356

I've also been having problems with this. It's funny because user_signed_in? is supposed to be more dependable than current_user, but this seems to indicate what I suspected -- that it's actually less dependable.

Let us know if someone finds something different.

Upvotes: 0

Wolfram Arnold
Wolfram Arnold

Reputation: 7273

Devise has a concept of "scopes" so that you can sign in multiple types of users simultaneously, e.g. an admin user and a regular user. To check if a user of any scope is signed in, use signed_in?. The scopes are defined by the naming of the model class and the routes.rb file. Be sure they match. Can you post both? To test this out you can also try an integration test.

Upvotes: 3

Related Questions