| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- # frozen_string_literal: true
- class OmniauthCallbacksController < ApplicationController
- include CookieBasedCsrf
- include UserAuthentication
- def create
- case params[:provider]
- when 'google_oauth2'
- google_oauth2
- when 'reddit'
- reddit
- when 'discord'
- discord
- else
- head :not_found
- end
- end
- private
- def google_oauth2
- @user = User.from_omniauth(request.env['omniauth.auth'])
- if @user.persisted?
- # this will throw if @user is not activated
- sign_in_and_redirect @user, event: :authentication
- # if is_navigational_format?
- # set_flash_message(:notice, :success, kind: 'Google')
- # end
- else
- redirect_to new_user_registration_url
- end
- end
- def reddit
- @user = User.from_omniauth(request.env['omniauth.auth'])
- if @user.persisted?
- # this will throw if @user is not activated
- sign_in_and_redirect @user, event: :authentication
- # if is_navigational_format?
- # set_flash_message(:notice, :success, kind: 'Reddit')
- # end
- else
- redirect_to new_user_registration_url
- end
- end
- def discord
- @user = User.from_omniauth(request.env['omniauth.auth'])
- if @user.persisted?
- # this will throw if @user is not activated
- sign_in_and_redirect @user, event: :authentication
- # if is_navigational_format?
- # set_flash_message(:notice, :success, kind: 'Discord')
- # end
- else
- redirect_to new_user_registration_url
- end
- end
- def sign_in_and_redirect(user, *_args)
- # Ensure we have a new CSRF token now that user is signed in
- cookies.delete(:_csrf_token)
- self.current_user = user
- cookies['x-csrf-token'] = {
- value: form_authenticity_token,
- httponly: false,
- secure: !(Rails.env.development? || Rails.env.test?),
- }
- redirect_to root_path
- end
- # More info at:
- # https://github.com/plataformatec/devise#omniauth
- # GET|POST /resource/auth/twitter
- # def passthru
- # super
- # end
- # GET|POST /users/auth/twitter/callback
- # def failure
- # super
- # end
- # protected
- # The path used when OmniAuth fails
- # def after_omniauth_failure_path_for(scope)
- # super(scope)
- # end
- end
|