新手问题 devise 的 current_user 小问题

cassiuschen · March 22, 2014 · Last by cassiuschen replied at March 22, 2014 · 2881 hits

用 Devise + Omniauth 在做 SSO,大部分代码抄自sso-devise-omniauth-provider,但是有个问题出现在Auth#user之前的action——Devise的:authenticate_user!方法……

class AuthController < ApplicationController
  before_action :authenticate_user!, :except => [:access_token]
  skip_before_action :verify_authenticity_token, :only => [:access_token]

  ... ...

  def user
    hash = {
      :provider => 'bdfzer',
      :id => current_user.id.to_s,
      :info => {
         :email      => current_user.email,
      },
      :extra => {
         :name => current_user.name,
         :pku_id => current_user.pku_id
      }
    }

    render :json => hash.to_json
  end

  ... ...

end

每次GET这个Auth#user的时候devise都会抛出说未登录,去掉before_action之后说current_user是NilClass对象,显然问题出在current_user上……

问题是在 user 方法被调用之前还调用了很多方法,都能查到 current_user,唯独 user 这里出问题,不知道问题是怎么发生的……

BTW 在 devise 源码中为啥找不到authenticate_user这部分的源码>~<而且一直有这个疑问 current_user 不是个局部变量么?devise 怎么做到在各处调用它的呢?

You need to Sign in before reply, if you don't have an account, please Sign up first.