1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
lpr-partynest/app/interactors/authenticate_user_with_omniauth.rb

51 lines
1.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class AuthenticateUserWithOmniauth
include Interactor
def call
validate_context
ActiveRecord::Base.transaction do
build_user
build_user_omniauth
save_records
end
end
private
def validate_context
return if context.provider.present? &&
context.remote_id.present? &&
context.email.present?
context.fail! user: nil, user_omniauth: nil
end
def build_user
context.user = User.where(
email: context.email,
).lock(true).first_or_initialize do |new_user|
new_user.password = Devise.friendly_token[0, 20]
end
end
def build_user_omniauth
context.user_omniauth = UserOmniauth.where(
provider: context.provider,
remote_id: context.remote_id,
).lock(true).first_or_initialize do |new_user_omniauth|
new_user_omniauth.user = context.user
new_user_omniauth.email = context.email
end
end
def save_records
return if context.user.save &&
context.user_omniauth.save
context.fail! user: nil, user_omniauth: nil
end
end