You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.3 KiB

  1. # frozen_string_literal: true
  2. class Auth::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  3. skip_before_action :verify_authenticity_token
  4. def self.provides_callback_for(provider)
  5. define_method provider do
  6. @user = User.find_for_oauth(request.env['omniauth.auth'], current_user)
  7. if @user.persisted?
  8. LoginActivity.create(
  9. user: @user,
  10. success: true,
  11. authentication_method: :omniauth,
  12. provider: provider,
  13. ip: request.remote_ip,
  14. user_agent: request.user_agent
  15. )
  16. sign_in_and_redirect @user, event: :authentication
  17. label = Devise.omniauth_configs[provider]&.strategy&.display_name.presence || I18n.t("auth.providers.#{provider}", default: provider.to_s.chomp('_oauth2').capitalize)
  18. set_flash_message(:notice, :success, kind: label) if is_navigational_format?
  19. else
  20. session["devise.#{provider}_data"] = request.env['omniauth.auth']
  21. redirect_to new_user_registration_url
  22. end
  23. end
  24. end
  25. Devise.omniauth_configs.each_key do |provider|
  26. provides_callback_for provider
  27. end
  28. def after_sign_in_path_for(resource)
  29. if resource.email_present?
  30. stored_location_for(resource) || root_path
  31. else
  32. auth_setup_path(missing_email: '1')
  33. end
  34. end
  35. end