OmniAuth Oauth2 没有将 Instagram 用户帐户保存到用户模型(设计)
OmniAuth Oauth2 not persisting Instagram user account to User model (Devise)
我正在使用 Omniauth gem 与 Devise gem 配对进行用户身份验证(这里是 wiki。我选择 Oauth2 策略在我的应用程序中对 Instagram 用户进行身份验证。
我的问题是通过 Instagram 身份验证登录的用户不会持久保存到我的用户模型中。在使用 Instagram 进行身份验证后,他们将被重定向到新的用户注册路径 localhost:3000/users/sign_up.
controllers/users/omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def instagram
# 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 => "Instagram") if is_navigational_format?
else
session["devise.instagram_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end
models/user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
devise :omniauthable, :omniauth_providers => [:instagram]
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
end
end
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.instagram_data"] && session["devise.instagram_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
end
用户 table 具有相应的列(provider
、uid
)
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "provider"
t.string "uid"
end
最后,这是通过 Instagram 身份验证登录时的输出
Started GET "/users/auth/instagram" for ::1 at 2015-09-25 09:57:54 -0400
I, [2015-09-25T09:57:54.577637 #7451] INFO -- omniauth: (instagram) Request phase initiated.
Started GET "/users/auth/instagram" for ::1 at 2015-09-25 09:57:54 -0400
I, [2015-09-25T09:57:54.655615 #7451] INFO -- omniauth: (instagram) Request phase initiated.
Started GET "/users/auth/instagram/callback?code=SOME_CODE&state=SOME_STATE" for ::1 at 2015-09-25 09:57:54 -0400
I, [2015-09-25T09:57:54.811861 #7451] INFO -- omniauth: (instagram) Callback phase initiated.
Processing by Users::OmniauthCallbacksController#instagram as HTML
Parameters: {"code"=>"SOME_CODE", "state"=>"SOME_STATE"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."provider" = ? AND "users"."uid" = ? ORDER BY "users"."id" ASC LIMIT 1 [["provider", "instagram"], ["uid", "343664764"]]
(0.1ms) begin transaction
(0.1ms) rollback transaction
Redirected to http://localhost:3000/users/sign_up
深入查看代码、添加调试器断点以跟踪从视图到控制器再到模型再返回控制器的数据流后,我意识到我的问题是 Instagram API 不提供电子邮件特定用户的。由于 Devise 默认需要电子邮件,而我为电子邮件 user.email = auth.info.email
分配了一个空白值,因此保存从未完成,我将被重定向到新用户注册路径 new_user_registration_url
.
我是怎么调试的
首先,我需要一条错误消息才能真正了解问题所在。所以,我加了一个“!”在 from_omniauth
class 方法中的 save!
之后。
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.save!
end
end
使用 save!
,验证总是 运行,如果我遇到任何失败,则会引发 ActiveRecord::ActiveRecordError。
然后错误消息指出电子邮件字段不能为空。
所以现在我实际上有更多的信息可以使用。我的解决方法不是很好,我不建议这样做 -- 但它解决了问题。
解决方法
我的解决方案是为每个通过 Instagram 注册的用户创建一个虚拟电子邮件。在我的 from_omniauth
方法中,我将用户电子邮件字段分配给 nickname
(从 Instagram 身份验证返回)字段和通用“@example.com”:
的串联
user.email = auth.info.nickname + "@example.com"
这不是一个可持续的解决方案,因为您最终会收到错误的电子邮件给每个注册 Instagram 的人(忘记通过 Action Mailer 向他们发送电子邮件)。
我还没有(但会)实施的更好的解决方案是将 signup/in 通过 Instagram 的用户重定向到特定的 URL,您将在其中提示用户输入他们的电子邮件地址。然后您可以发回该信息,以便控制器可以处理并正确保存。
当然,这是用户可能不愿意执行的额外步骤,但是电子邮件地址是一种宝贵的资产,可用于做很多事情,例如时事通讯和其他促销电子邮件。
我正在使用 Omniauth gem 与 Devise gem 配对进行用户身份验证(这里是 wiki。我选择 Oauth2 策略在我的应用程序中对 Instagram 用户进行身份验证。
我的问题是通过 Instagram 身份验证登录的用户不会持久保存到我的用户模型中。在使用 Instagram 进行身份验证后,他们将被重定向到新的用户注册路径 localhost:3000/users/sign_up.
controllers/users/omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def instagram
# 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 => "Instagram") if is_navigational_format?
else
session["devise.instagram_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end
models/user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
devise :omniauthable, :omniauth_providers => [:instagram]
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
end
end
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.instagram_data"] && session["devise.instagram_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
end
用户 table 具有相应的列(provider
、uid
)
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "provider"
t.string "uid"
end
最后,这是通过 Instagram 身份验证登录时的输出
Started GET "/users/auth/instagram" for ::1 at 2015-09-25 09:57:54 -0400
I, [2015-09-25T09:57:54.577637 #7451] INFO -- omniauth: (instagram) Request phase initiated.
Started GET "/users/auth/instagram" for ::1 at 2015-09-25 09:57:54 -0400
I, [2015-09-25T09:57:54.655615 #7451] INFO -- omniauth: (instagram) Request phase initiated.
Started GET "/users/auth/instagram/callback?code=SOME_CODE&state=SOME_STATE" for ::1 at 2015-09-25 09:57:54 -0400
I, [2015-09-25T09:57:54.811861 #7451] INFO -- omniauth: (instagram) Callback phase initiated.
Processing by Users::OmniauthCallbacksController#instagram as HTML
Parameters: {"code"=>"SOME_CODE", "state"=>"SOME_STATE"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."provider" = ? AND "users"."uid" = ? ORDER BY "users"."id" ASC LIMIT 1 [["provider", "instagram"], ["uid", "343664764"]]
(0.1ms) begin transaction
(0.1ms) rollback transaction
Redirected to http://localhost:3000/users/sign_up
深入查看代码、添加调试器断点以跟踪从视图到控制器再到模型再返回控制器的数据流后,我意识到我的问题是 Instagram API 不提供电子邮件特定用户的。由于 Devise 默认需要电子邮件,而我为电子邮件 user.email = auth.info.email
分配了一个空白值,因此保存从未完成,我将被重定向到新用户注册路径 new_user_registration_url
.
我是怎么调试的
首先,我需要一条错误消息才能真正了解问题所在。所以,我加了一个“!”在 from_omniauth
class 方法中的 save!
之后。
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.save!
end
end
使用 save!
,验证总是 运行,如果我遇到任何失败,则会引发 ActiveRecord::ActiveRecordError。
然后错误消息指出电子邮件字段不能为空。
所以现在我实际上有更多的信息可以使用。我的解决方法不是很好,我不建议这样做 -- 但它解决了问题。
解决方法
我的解决方案是为每个通过 Instagram 注册的用户创建一个虚拟电子邮件。在我的 from_omniauth
方法中,我将用户电子邮件字段分配给 nickname
(从 Instagram 身份验证返回)字段和通用“@example.com”:
user.email = auth.info.nickname + "@example.com"
这不是一个可持续的解决方案,因为您最终会收到错误的电子邮件给每个注册 Instagram 的人(忘记通过 Action Mailer 向他们发送电子邮件)。
我还没有(但会)实施的更好的解决方案是将 signup/in 通过 Instagram 的用户重定向到特定的 URL,您将在其中提示用户输入他们的电子邮件地址。然后您可以发回该信息,以便控制器可以处理并正确保存。
当然,这是用户可能不愿意执行的额外步骤,但是电子邮件地址是一种宝贵的资产,可用于做很多事情,例如时事通讯和其他促销电子邮件。