【问题标题】:Rails + Devise: if user_signed_in? not workingRails + 设计:如果 user_signed_in?不工作
【发布时间】:2015-07-06 21:03:53
【问题描述】:

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

_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)

【问题讨论】:

  • 请在您尝试登录时显示 rails 日志输出
  • Started GET "/" 是否意味着您已重定向到登录页面?
  • @asiniy 我相信这意味着它被重定向到 root_path。
  • 您是否使用 OmniAuth 登录?如果是,您是否在您的用户模型中实现了self.from_omniauth 方法? OmniAuth: Overview
  • 我之前使用过 OmniAuth,但为了调试而将其注释掉

标签: ruby-on-rails devise omniauth


【解决方案1】:

您想首先对您的身份验证用户设置一个例外吗?这样,它甚至不会在 current_user/@user/etc 设置之前尝试运行身份验证。例如,如果您的根是索引:

before_action :authenticate_user!, :except =&gt; [:index]

然后 - 确保拥有 better_errors gem 并在您的 if user_signed_in? 语句中加入一些废话,刷新页面以触发浏览器中的控制台。看看@usercurrent_user 或你正在使用的东西是否一开始就设置好了。然后我会从那里向后调试。

https://github.com/charliesome/better_errors

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

Rails devise: user_signed_in? not working

【讨论】:

  • 问题不在于before_action :authenticate_user! - 而是用户永远无法真正登录,即使使用正确的凭据。
猜你喜欢
  • 2011-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
  • 2016-02-22
相关资源
最近更新 更多