我确实看过Ryan Bates剧集以使用omniauth设计。问题是我能够使用linkdin注册。我的代码
在我的user.rb
中field :provider, :type => String
field :uid, :type => String
field :name, :type => String
#has_many :authentications
def self.from_omniauth(auth)
where(auth.slice("provider", "uid")).first || create_from_omniauth(auth)
end
def self.create_from_omniauth(auth)
create! do |user|
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["info"]["nickname"]
end
end
我添加了这个并在我的创建控制器中进行身份验证
user = User.from_omniauth(env["omniauth.auth"])
session[:user_id] = user.id
redirect_to root_url, notice: "Signed in!"
我成功将linkdin的值放在我的用户数据库中
{ "_id" : ObjectId("50b2f4e66d7ab88ac7000003"), "email" : "", "encrypted_password" : "", "sign_in_count" : 0, "provider" : "linkedin", "uid" : "wuBFLcbDyB", "name" : null, "updated_at" : ISODate("2012-11-26T04:49:42.549Z"), "created_at" : ISODate("2012-11-26T04:49:42.549Z") }
但是当我从linkdin登录时,它不会通过linkdin注册,否则会重定向到
http://localhost:3000/users/sign_in
如何通过该链接登录?
答案 0 :(得分:1)
如果您的用户模型中有类似的内容
validates :username, presence: true
然后您必须知道链接在内未提供任何用户名。因此,要完成身份验证/注册,您的用户必须明确添加其用户名。
确保您的registrations_contreoller.rb看起来像这样
class RegistrationsController < Devise::RegistrationsController
def create
super
end
private
def build_resource(*args)
super
if session[:omniauth]
@user.apply_omniauth(session[:omniauth])
@user.valid?
end
end
end