我在使用omniauth-facebook 时遇到问题。我尝试使用 facebook 登录我的 Rails 应用程序,但它给了我一个错误,并且 env['omniauth.auth'] 在这里抛出一个 nil 值是错误..
NoMethodError at /auth/facebook/callback
undefined method `slice' for nil:NilClass
这是我的模型
#fields
field :provider
field :uid
field :name
field :oauth_token
field :oauth_expires_at , type: DateTime
#functions
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).find_or_initialize_by.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
这是我的控制器
def create
user = Usersfb.from_omniauth(ENV["omniauth.auth"])
session[:user_id] = user.id
redirect_to root_url
end
def destroy
session[:user_id] = nil
redirect_to root_url
end
我的路线.rb
root 'main#index'
get 'session/destroy' , to: 'session#destroy'
get 'auth/:provider/callback' => 'session#create'