我已經在設計之上使用Google,Linkedin,Dropbox和Github設置了更簡單的社交用戶認證。
的的Dropbox認證不起作用,而是給出了回調URL
(http://localhost:3000/users/auth/dropbox/callback)的錯誤:Rails 4:用戶身份驗證 - NoMethodError
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
我感謝每個幫助!提前致謝。
我認爲這個問題是在用戶模式的第5行,因爲錯誤仍然存在後我結合了你的建議。它似乎爲Dropbox它以某種方式無法找到一個網址。 – jonhue
所以,同樣的原則適用。你在'auth.info.urls'上調用'.first',這意味着它是空的或者不存在。在這一行之前使用pry或byebug來調試使用pry或byebug來停止你的程序並檢查auth是否實際返回,然後相應地調整你的代碼。 –