我将devise_token_auth用于电子邮件和基于 Google Oauth2 的身份验证(omniauth-google-oauth2
为此使用了 gem)。我已经成功地通过 Google Oauth2 流程存储了用户注册/登录的登录信息。信息包括:
{"auth_token"=>"token here", "client_id"=>"client id here", "uid"=>"uid here", "expiry"=>1620492005, "config"=>nil, "oauth_registration"=>true}
上述信息的流程是
- 访问 http://localhost:3000/auth/google_oauth2。这会将您重定向到 Google 身份验证屏幕
- 用户选择帐户并授予权限。
- 我的应用程序的 Oauth 成功回调在 http://localhost:3000/auth/google_oauth2/callback 执行
第一步执行的代码是
module DeviseTokenAuth
class OmniauthCallbacksController < DeviseTokenAuth::ApplicationController
attr_reader :auth_params
before_action :validate_auth_origin_url_param
def omniauth_success
get_resource_from_auth_hash
set_token_on_resource
create_auth_params
if confirmable_enabled?
# don't send confirmation email!!!
@resource.skip_confirmation!
end
sign_in(:user, @resource, store: false, bypass: false)
@resource.save!
yield @resource if block_given?
render_data_or_redirect('deliverCredentials', @auth_params.as_json, @resource.as_json)
end
end
end
我面临的问题:
sign_in
@current_user
尽管如此,方法调用并没有设置,@resource
并且其中@auth_params
包含所有必要的信息。- 如何通知我的前端应用程序有关登录信息(令牌,client_id,uid)?
render_data_or_redirect('deliverCredentials', @auth_params.as_json, @resource.as_json)
此调用不会重定向或呈现任何内容,而是停留在同一页面上,它显示的 URL 是 http://localhost:3000/auth/google_oauth2/callback#
我现在基本上有三个问题:
- 如何使用基于传入的身份验证标头
devise_token_auth
设置?current_user
我已将以下行添加到我的控制器中,但仍然无法设置@current_user
include DeviseTokenAuth::Concerns::SetUserByToken
也许是因为我错误地发送了身份验证标头?请参阅下面的第三点。
我应该如何将登录信息发送到我的前端应用程序?我是否以某种方式修改上述方法以将登录信息发送到我的前端应用程序?
为了发出经过身份验证的请求,我应该在哪里放置 auth 标头?
- 当使用
devise_token_auth
电子邮件作为身份验证提供者时,我必须发送 3 件来发出经过身份验证的请求,access-token
即client_id
uid
- 现在,对于 Google/Facebook 等提供商,我是否设置所有这些标头?
我已经使用邮递员测试了以下两项,但由于未经授权的错误而失败
- 已发送
access-token
,client_id
并uid
在标头中 - 发送
Bearer my_token
授权标头。