我拥有omniauth 并成功地为LinkedIn 和Twitter 设计了设置。
使用LinkedIn进行身份验证时,我能够自动将用户名和电子邮件传递到注册表单中-但它仍然给出错误并要求用户“检查错误”,这在技术上是错误的用户体验没有错误。
如何在创建过程中将它们自动分配给用户,但只有当它们存在时?以下是相关代码:
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def all
user = User.from_omniauth(request.env["omniauth.auth"])
if user.persisted?
flash.notice = "Signed in!"
sign_in_and_redirect user
else
session["devise.user_attributes"] = user.attributes
flash.notice = "Please confirm your name and email"
redirect_to sign_up_path
end
end
alias_method :linkedin, :all
alias_method :twitter, :all
end
从用户模型:
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.email = auth.info.email
end
end