* Add Authentication providers * Making strides on refactoring * New usr from Twitter * Existing Twitter user * Existing user Github * Test skip_confirmation! * Refactor a bit and add simplecov groups * Test user already logged in * Rename to Authentication::Authenticator * Refactor to a common class * Add existing user test for twitter login * Refactor global handler * Fix spec and comment Twitter provider * Update comments and strategy TODO * Move providers loading code to Authentication::Providers * add SubclassResponsibility error * Use delegation
81 lines
2.8 KiB
Ruby
81 lines
2.8 KiB
Ruby
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
|
|
LEGACY_COOKIE_NAME = "_PracticalDeveloper_session".freeze
|
|
|
|
# Don't need a policy for this since this is our sign up/in route
|
|
include Devise::Controllers::Rememberable
|
|
|
|
def twitter
|
|
callback_for("twitter")
|
|
end
|
|
|
|
def github
|
|
callback_for("github")
|
|
end
|
|
|
|
def failure
|
|
error = request.env["omniauth.error"]
|
|
class_name = error.present? ? error.class.name : ""
|
|
|
|
DatadogStatsClient.increment(
|
|
"omniauth.failure",
|
|
tags: [
|
|
"class:#{class_name}",
|
|
"message:#{error&.message}",
|
|
"reason:#{error.try(:error_reason)}",
|
|
"type:#{error.try(:error)}",
|
|
"uri:#{error.try(:error_uri)}",
|
|
"provider:#{request.env['omniauth.strategy'].name}",
|
|
"origin:#{request.env['omniauth.strategy.origin']}",
|
|
"params:#{request.env['omniauth.params']}",
|
|
],
|
|
)
|
|
|
|
super
|
|
end
|
|
|
|
private
|
|
|
|
# TODO: [thepracticaldev/oss] test all of this
|
|
def callback_for(provider)
|
|
cta_variant = request.env["omniauth.params"]["state"].to_s
|
|
@user = Authentication::Authenticator.call(
|
|
request.env["omniauth.auth"],
|
|
current_user: current_user,
|
|
cta_variant: cta_variant,
|
|
)
|
|
|
|
if persisted_and_valid?
|
|
# delete legacy session based cookie once the user has logged in again
|
|
request.cookie_jar.delete(LEGACY_COOKIE_NAME) if request.cookies[LEGACY_COOKIE_NAME]
|
|
|
|
remember_me(@user)
|
|
sign_in_and_redirect @user, event: :authentication
|
|
set_flash_message(:notice, :success, kind: provider.to_s.capitalize) if is_navigational_format?
|
|
elsif persisted_but_username_taken?
|
|
redirect_to "/settings?state=previous-registration"
|
|
else
|
|
session["devise.#{provider}_data"] = request.env["omniauth.auth"]
|
|
user_errors = @user.errors.full_messages
|
|
Rails.logger.error "Log in error: sign in failed. username: #{@user.username} - email: #{@user.email}"
|
|
Rails.logger.error "Log in error: auth data hash - #{request.env['omniauth.auth']}"
|
|
Rails.logger.error "Log in error: auth data hash - #{request.env['omniauth.error']&.inspect}"
|
|
Rails.logger.error "Log in error: user_errors: #{user_errors}"
|
|
flash[:alert] = user_errors
|
|
redirect_to new_user_registration_url
|
|
end
|
|
rescue StandardError => e
|
|
Rails.logger.error "Log in error: #{e}"
|
|
Rails.logger.error "Log in error: auth data hash - #{request.env['omniauth.auth']}"
|
|
Rails.logger.error "Log in error: auth data hash - #{request.env['omniauth.error']&.inspect}"
|
|
flash[:alert] = "Log in error: #{e}"
|
|
redirect_to new_user_registration_url
|
|
end
|
|
|
|
def persisted_and_valid?
|
|
@user.persisted? && @user.valid?
|
|
end
|
|
|
|
def persisted_but_username_taken?
|
|
@user.persisted? && @user.errors.full_messages.join(", ").include?("username has already been taken")
|
|
end
|
|
end
|