Incerteza
Incerteza

Reputation: 34934

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

# 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))

What's the best way to catch the moment when the user gets redirected to google for authentication?

It could be by "javascript onclick", but I want to catch the moment on the server somehow (if it's possible) because I need to save some variable to the session.

Upvotes: 0

Views: 551

Answers (1)

djsmentya
djsmentya

Reputation: 315

There is a list of init callbacks that can be used. I guess you need OmniAuth.config.before_request_phase

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

Upvotes: 1

Related Questions