全身份验证 - 抓住用户被重定向到谷歌的时刻

Omniauth authentication - catch the moment when the user gets redirected to google

本文关键字:重定向 谷歌 时刻 用户 身份验证      更新时间:2023-09-26
# Gemfile
gem 'omniauth'
gem 'omniauth-google-oauth2'
gem 'devise'
# view
- if user_signed_in?
  Signed in as 
  = current_user.oauth_name
  = button_to('Sign out', destroy_user_session_path, method: :delete)
- else
  = link_to('Sign in with Google', user_omniauth_authorize_path(:google_oauth2))

捕捉用户被重定向到谷歌进行身份验证的最佳方法是什么?

它可能是通过"javascript onclick",但我想以某种方式在服务器上捕捉时刻(如果可能的话),因为我需要将一些变量保存到会话中。

有一个可以使用的初始化回调列表。我想你需要OmniAuth.config.before_request_phase

#config/initializers/omniauth.rb
OmniAuth.config.before_request_phase do |env|
  env['omniauth.auth']
  # Your code there
end