我正在使用 Omniauth 尝试在我的网络应用程序上注册/登录用户。这发生在我的AuthenticationsController#Create
方法中:
身份验证控制器:
class AuthenticationsController < InheritedResources::Base
def create
omniauth = request.env['omniauth.auth']
authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
if authentication
flash[:success] = "Signed in successfully"
sign_in_and_redirect(authentication.user)
elsif current_user
token = omniauth['credentials'].token
secret = omniauth['credentials'].secret
current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'], :token => token, :secret => token_secret)
flash[:success] = "Authentication successful"
redirect_to authentications_url
else
user = User.new
user.apply_omniauth(omniauth)
if user.save!
flash[:success] = "Account created"
sign_in(authentication.user)
else
session[:omniauth] = omniauth.except('extra')
redirect_to '/signup'
end
end
end
我最初有sign_in(:user, authentication.user)
,但它给了我参数错误,所以我把它改成had sign_in(authentication.user)
了上面的代码。但是,现在我得到一个NoMethodError - undefined method 'user' for nil:NilClass
.