Google Rails 上的身份验证错误 Ruby
Google Authentication error Ruby on Rails
我正在尝试在我的 Rails 应用程序上实施 Google 身份验证,但我遇到了一些问题。
在 https://richonrails.com/articles/google-authentication-in-ruby-on-rails/ 之后,我使用从 Google 的开发者控制台 'omniauth.rb'
获得的密钥创建了我的初始化程序
OmniAuth.config.logger = Rails.logger
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, '*****', '*****', {client_options: {ssl: {ca_file: Rails.root.join("cacert.pem").to_s}}}
end
我添加了一些路线
# GOOGLE AUTH
get 'auth/:provider/callback', to: 'sessions#create'
get 'auth/failure', to: redirect('/')
get 'signout', to: 'sessions#destroy', as: 'signout'
我的会话的创建操作
def create
user = User.from_omniauth(env["omniauth.auth"])
sign_in user
flash[:success] = 'Logged in!'
redirect_to root_path
end
以及用户模型中的User.from_omniauth
方法
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
user.password = user.password_confirmation = user.password_digest = SecureRandom.urlsafe_base64(n=6)
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
我设置了一个随机密码,因为我也在使用 bcrypt 身份验证,它强制每个用户都有一个密码
最后,在我看来我有一个登录按钮
= link_to "Sign in with Google", "/auth/google_oauth2", id: "sign_in", class: "btn btn-primary"
问题是它不起作用,当我单击它时,会话创建时显示错误 user = User.from_omniauth(env["omniauth.auth"])
:
NameError at /auth/google_oauth2/callback
undefined local variable or method `env' for #<SessionsController:0x956eb90>
有时它会在另一行中抛出 SSL_connect SYSCALL returned=5 errno=0 state=SSLv2/v3 read server hello A (OpenSSL::SSL::SSLError)
,但我想这是另一个错误。
没有 env
方法可用。
而不是这个,使用
user = User.from_omniauth(request.env["omniauth.auth"])
在您的创建方法中,因为 request.env["omniauth.auth"]
对象具有 google 在对您的应用程序进行用户身份验证后发送的信息。
我正在尝试在我的 Rails 应用程序上实施 Google 身份验证,但我遇到了一些问题。
在 https://richonrails.com/articles/google-authentication-in-ruby-on-rails/ 之后,我使用从 Google 的开发者控制台 'omniauth.rb'
获得的密钥创建了我的初始化程序OmniAuth.config.logger = Rails.logger
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, '*****', '*****', {client_options: {ssl: {ca_file: Rails.root.join("cacert.pem").to_s}}}
end
我添加了一些路线
# GOOGLE AUTH
get 'auth/:provider/callback', to: 'sessions#create'
get 'auth/failure', to: redirect('/')
get 'signout', to: 'sessions#destroy', as: 'signout'
我的会话的创建操作
def create
user = User.from_omniauth(env["omniauth.auth"])
sign_in user
flash[:success] = 'Logged in!'
redirect_to root_path
end
以及用户模型中的User.from_omniauth
方法
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
user.password = user.password_confirmation = user.password_digest = SecureRandom.urlsafe_base64(n=6)
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
我设置了一个随机密码,因为我也在使用 bcrypt 身份验证,它强制每个用户都有一个密码
最后,在我看来我有一个登录按钮
= link_to "Sign in with Google", "/auth/google_oauth2", id: "sign_in", class: "btn btn-primary"
问题是它不起作用,当我单击它时,会话创建时显示错误 user = User.from_omniauth(env["omniauth.auth"])
:
NameError at /auth/google_oauth2/callback
undefined local variable or method `env' for #<SessionsController:0x956eb90>
有时它会在另一行中抛出 SSL_connect SYSCALL returned=5 errno=0 state=SSLv2/v3 read server hello A (OpenSSL::SSL::SSLError)
,但我想这是另一个错误。
没有 env
方法可用。
而不是这个,使用
user = User.from_omniauth(request.env["omniauth.auth"])
在您的创建方法中,因为 request.env["omniauth.auth"]
对象具有 google 在对您的应用程序进行用户身份验证后发送的信息。