使用设计和导轨的omniauth叽叽喳喳宝石5
我开始看Ryan Bates的tutorial关于如何在设计中使用Omniauth(rails-cast 235修订版)。我遇到了user.rb文件的问题。他显示了他tutorial使用设计和导轨的omniauth叽叽喳喳宝石5
devise :omniauthable, # ...
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.username = auth.info.nickname
end
end
def self.new_with_session(params, session)
if session["devise.user_attributes"]
new(session["devise.user_attributes"], without_protection: true) do |user|
user.attributes = params
user.valid?
end
else
super
end
end
def password_required?
super && provider.blank?
end
def update_with_password(params, *options)
if encrypted_password.blank?
update_attributes(params, *options)
else
super
end
end
不工作对我来说,它的显示加载ActiveModel :: ForbiddenAttributesError与这行代码where(auth.slice(:provider, :uid)).first_or_create do |user|
突出。我想他的版本不工作,因为我使用Rails 5.反正我已经尝试修改user.rb文件。用下面的代码。
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
\t user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.provider = auth.provider
user.uid = auth.uid
token = auth.credentials.token,
secret = auth.credentials.secret
end
end
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.twitter_data"]
# user.attributes = params
user.update(
email: params[:email],
password: Devise.friendly_token[0,20],
provider: data["provider"],
uid: data["token"],
token: data["credentials"]["token"],
secret: data["credentials"]["secret"]
)
end
end
end
def password_required?
super && provider.blank?
end
def update_with_password(params, *options)
if encrypted_password.blank?
update_attributes(params, *options)
else
super
end
end
end
我能够获得Twitter的身份验证页面显示了负载了大约一分钟,然后重定向回我的用户注册页面,而不登录,也没有显示任何错误消息。顺便说一下,有人知道如何使用设计与Facebook登录。
该问题与您传递给self.from_omniauth(auth)
的属性auth
有关。你应该设置你的服务器日志binding.pry
或puts auth
所以看到这个变量的样子..
也许当self.from_amniauth(auth)
方法运行此auth.slice()
它在某种程度上错误运行
另外,first_or_create
方法可以创建用户你可能会错过一些东西,引发错误。所以尝试使用调试显示使用user.errors.full_messages
想法听到的是,该方法的目的是找到User
或创建它。它正在从控制器动作Users::OmniauthCallbacksController#facebook
request
意味着这是从你的服务器facebook
或twitter
卸任request
叫..
Facebook或Twitter将回复此request
与AJAX response
,其中将包括信息你需要认证用户。
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?, you probably saved the env variable
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def failure
redirect_to root_path
end, you probably saved the env variable
end
然后用下面的方法where(provider: auth.provider, uid: auth.uid).first_or_create
你要么在user
或find
它从你的数据库创建。问题是你可能从https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
编写这个方法错误或错误的参数调用它,或者你没有建立
api
与Twitter正常工作......这个动作有值得描述的几个方面:
OmniAuth从Facebook检索到的所有信息均可作为request.env [“omniauth.auth”]的散列使用。检查OmniAuth文档和每个omniauth-facebook gem的README,以了解哪些信息正在返回。
找到有效用户后,可以使用两种Devise方法之一登录:sign_in或sign_in_and_redirect。传递:event =>:认证是可选的。你应该这样做,如果你想使用守望者回调。
也可以使用Devise的默认消息之一设置一个flash消息,但这取决于您。
如果用户未被保存,我们将OmniAuth数据存储在会话中。注意我们使用“设计”来存储这些数据。作为键名称空间。这很有用,因为Devise删除了所有以“设计”开头的数据。无论何时用户登录,都会从会话中清除,因此我们会自动清理会话。最后,我们将用户重定向到我们的注册表单。
控制器被定义之后,我们需要实现from_omniauth方法在我们的模型(例如应用程序/模型/ user.rb):
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image
# If you are using confirmable and the provider(s) you use validate emails,
# uncomment the line below to skip the confirmation emails.
# user.skip_confirmation!
end
end
这种方法试图找到现有用户由提供者和uid字段。如果找不到用户,则使用随机密码和一些额外信息创建一个新用户。请注意,first_or_create方法在创建新用户时自动设置提供者和uid字段。 first_or_create!方法的操作方式相似,只是如果用户记录未通过验证,它将引发异常。