【发布时间】:2017-04-21 15:44:19
【问题描述】:
我使用 Google、Linkedin、Dropbox 和 Github 在设计之上设置了一个更简单的社交用户身份验证。
Dropbox 身份验证不起作用,而是在回调 URL
(http://localhost:3000/users/auth/dropbox/callback) 上显示该错误:
NoMethodError in Users::OmniauthCallbacksController#dropbox
undefined method `first' for nil:NilClass
问题: 用户模型(第 8 行)
我的代码:
回调控制器:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def all
user = User.from_omniauth(env['omniauth.auth'], current_user)
if user.persisted?
sign_in user
flash[:notice] = t('devise.omniauth_callbacks.success', :kind => User::SOCIALS[params[:action].to_sym])
if user.sign_in_count == 1
redirect_to edit_user_registration_path
else
redirect_to root_path
end
else
session['devise.user_attributes'] = user.attributes
redirect_to new_user_registration_url
end
end
User::SOCIALS.each do |k, _|
alias_method k, :all
end
end
用户模型:
# omniauth Gem
def self.from_omniauth(auth, current_user)
authorization = Authorization.where(:provider => auth.provider, :uid => auth.uid.to_s,
:token => auth.credentials.token,
:secret => auth.credentials.secret).first_or_initialize
authorization.profile_page = auth.info.urls.first.last unless authorization.persisted?
if authorization.user.blank?
user = current_user.nil? ? User.where('email = ?', auth['info']['email']).first : current_user
if user.blank?
user = User.new
user.skip_confirmation!
user.password = Devise.friendly_token[0, 20]
user.fetch_details(auth)
user.save
end
authorization.user = user
authorization.save
end
authorization.user
end
def fetch_details(auth)
self.email = auth.info.email
self.username = auth.info.name
self.avatar = URI.parse(auth.info.image)
end
感谢每一个帮助!提前致谢。
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 authentication devise