【发布时间】:2021-12-27 11:03:44
【问题描述】:
我希望你很好。我尝试对我的 Rails API 实施谷歌身份验证。我得到错误
ActiveRecord::RecordInvalid (Validation failed: Email is invalid)
我的用户.rb:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:confirmable, :trackable,
:omniauthable, omniauth_providers: [:google_oauth2]
def self.create_user_for_google(data)
where(uid: data['email']).first_or_initialize.tap do |user|
user.provider = 'google_oauth2'
user.uid = data['email']
user.email = data['email']
user.password = Devise.friendly_token[0, 20]
user.password_confirmation = user.password
user.save!
end
end
end
我的 users_controller.rb 看起来:
class UsersController < ApplicationController
def google_oauth2
url = "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=#{params['id_token']}"
response = HTTParty.get(url, format: :plain)
user = User.create_user_for_google(response.parsed_response)
render json: user
end
end
【问题讨论】:
-
你能分享
HTTParty.get(url, format: :plain)的回复吗?
标签: ruby-on-rails ruby authentication