我正在尝试在我的 Rails API 中实现从谷歌功能登录。
我将此行添加到我的 device.rb
config.omniauth :google_oauth2, Rails.application.credentials.dig(:google, :google_client_id),
Rails.application.credentials.dig(:google, :google_client_secret), scope: 'userinfo.email,userinfo.profile'
我还在我的凭据中添加了客户端 ID 和密码。
这是我的谷歌功能
def google
@user = User.from_omniauth(request.env["omniauth.auth"])
end
这是我来自 User.rb 的 from_omniauth 函数
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
end
end
在实现 API 时,我不知道应该使用request.env["omniauth.auth"]
还是硬编码从 OAuthPlayground 获得的访问令牌。如果我对令牌进行硬编码,如何从该令牌访问用户信息?
我还在我的应用程序中使用 Devise for Authentication,遵循本教程: https ://www.digitalocean.com/community/tutorials/how-to-configure-devise-and-omniauth-for-your-rails-application