Rails + Devise:if user_signed_in?不工作

时间:2015-07-06 21:03:53

标签: ruby-on-rails devise omniauth

我首先发现了一个问题,即用户没有使用此逻辑登录:

_header.html.erb

<% if user_signed_in? %>
          <li><%= link_to "Log out", destroy_user_session_path, method: :delete %></li>
        <% else %>
          <li><%= link_to "Sign in", new_user_session_path %></li>
        <% end %>

然后我尝试将其添加到 application_controller.rb

before_filter :authenticate_user!

我不断回到登录页面(即使使用有效的凭据)。

好像我的用户会话无法正常工作 - 虽然我可以在RailsAdmin控制台上看到登录计数和上次登录日期显示为正在登录。

这是我的 user.rb

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

  belongs_to :company

  has_and_belongs_to_many :productlines
end

我的 routes.rb

Rails.application.routes.draw do

  mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
  devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }

  root 'productlines#index'

end

omniauth_callbacks_controller.rb

class OmniauthCallbacksController < Devise::OmniauthCallbacksController 
  def google_oauth2
    @user = User.from_omniauth(request.env["omniauth.auth"])
    if @user.persisted?
      flash.notice = "Signed in through Google"
      sign_in_and_redirect @user
      return
    else
      session["devise.user_attributes"] = @user.attributes
      flash.notice = "You are almost Done! Please provide a password to finish setting up your account"
      redirect_to new_user_registration_path
    end
  end
end

更新:这是我的 application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  skip_before_filter :verify_authenticity_token, :if => Proc.new { |c| c.request.format == 'application/json' }
  before_filter :productline

  def productline
    @productlines = Productline.all 
  end

end

每次登录时,我都会重新路由回root_path,并且仍会显示“登录”链接。

编辑:点击登录时的日志输出:

Started POST "/users/sign_in" for ::1 at 2015-07-06 23:20:15 -0400
Processing by Devise::SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"6Eh4Qw3qErGmsppatErFZYhTOZHs8DhCOMXqGAMrBzRdTd72L5rIGAChLDvnI/GzOv1kQsyL43o/B6AQQtnk4Q==", "user"=>{"email"=>"broy@bullhorn.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."email" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["email", "b@gmail.com"]]
   (0.1ms)  begin transaction
  SQL (0.3ms)  UPDATE "users" SET "last_sign_in_at" = ?, "current_sign_in_at" = ?, "sign_in_count" = ?, "updated_at" = ? WHERE "users"."id" = ?  [["last_sign_in_at", "2015-07-07 03:17:08.826634"], ["current_sign_in_at", "2015-07-07 03:20:15.963289"], ["sign_in_count", 93], ["updated_at", "2015-07-07 03:20:15.964239"], ["id", 4]]
   (1.5ms)  commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 73ms (ActiveRecord: 2.1ms)


Started GET "/" for ::1 at 2015-07-06 23:20:15 -0400
Processing by ProductlinesController#index as HTML
  Productline Load (0.1ms)  SELECT "productlines".* FROM "productlines"
  Rendered productlines/index.html.erb within layouts/application (2.1ms)
  Rendered layouts/_header.html.erb (1.7ms)
Completed 200 OK in 48ms (Views: 47.3ms | ActiveRecord: 0.1ms)


Started GET "/" for ::1 at 2015-07-06 23:20:16 -0400
Processing by ProductlinesController#index as HTML
  Productline Load (0.2ms)  SELECT "productlines".* FROM "productlines"
  Rendered productlines/index.html.erb within layouts/application (104.8ms)
  Rendered layouts/_header.html.erb (1.1ms)
Completed 200 OK in 155ms (Views: 154.1ms | ActiveRecord: 0.2ms)

1 个答案:

答案 0 :(得分:0)

您是否希望首先在您的身份验证用户中添加例外?这样,在设置current_user / @ user / etc之前,它不会尝试运行身份验证。例如,如果您的根是索引:

before_action :authenticate_user!, :except => [:index]

然后 - 确保拥有better_errors gem并在if user_signed_in?语句中抛出一些废话乱码,刷新页面以在浏览器中触发控制台。首先查看@usercurrent_user或您正在使用的内容。然后我会从那里向后调试。

https://github.com/charliesome/better_errors

最后,这是一个stackoverflow链接我遇到了类似的问题,下面有几个答案:

Rails devise: user_signed_in? not working