!我试图让同事和学生使用他们的谷歌项目帐户注册。我已经获得了令牌,但后来我得到了一个错误。
app / controllers / authentications_controller.rb:5:在'create'
中我的身份验证控制器创建方法
def create
auth = request.env[":google_oauth2"]
@user = User.find_for_google_oauth2(request.env["omniauth.auth"])
redirect_to root_url, :notice => "Signed in!"
end
我的用户模型
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.first_name = auth.info.first_name
user.last_name = auth.info.last_name # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image
end
end
def self.find_for_google_oauth2(access_token, signed_in_resource=nil)
data = access_token.info
user = User.where(:email => data["email"]).first
unless user
user = User.create(name: data["name"],
email: data["email"],
uid: access_token.uid,
provider: access_token.provider,
password: Devise.friendly_token[0,20]
)
end
user
end
和我的config / omniauth.rd
require 'omniauth-google-oauth2'
OmniAuth.config.logger = Rails.logger
OmniAuth.logger.progname = "omniauth"
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, '***********petnk.apps.googleusercontent.com',
google_client_secret: '***********',
prompt: "consent",
select_account: true,
scope: 'userinfo.email',
image_aspect_ratio: 'square',
image_size: 50
on_failure { |env|AuthenticationsController.action(:failure).call(env) }
end
不确定要包含什么来帮助我们帮助我。我是铁道新手,并且一直在努力学习过去的一年,所以感谢任何帮助。
感谢
答案 0 :(得分:0)
我必须将我的创建功能从我的身份验证控制器移动到我的omnicontroller。不确定为什么这解决了这个问题,但现在确实如此。