Skip to content

Commit 8ba8a67

Browse files
committed
Flatten logic
1 parent 623f578 commit 8ba8a67

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

dashboard/app/controllers/omniauth_callbacks_controller.rb

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,29 @@ def connect_provider
2727
provider = auth_hash.provider.to_s
2828
return head(:bad_request) unless AuthenticationOption::OAUTH_CREDENTIAL_TYPES.include? provider
2929

30-
# Check if credential is already in use
3130
existing_credential_holder = User.find_by_credential type: provider, id: auth_hash.uid
31+
32+
# Credential is already held by the current user
33+
# Notify of no-op.
34+
if existing_credential_holder&.==(current_user)
35+
flash.notice = I18n.t('auth.already_linked', provider: I18n.t("auth.#{provider}"))
36+
return redirect_to edit_user_registration_path
37+
end
38+
39+
# Credential is already held by another user with activity
40+
# Display an error explaining that the credential is already in use.
41+
if existing_credential_holder&.has_activity?
42+
flash.alert = I18n.t('auth.already_in_use', provider: I18n.t("auth.#{provider}"))
43+
return redirect_to edit_user_registration_path
44+
end
45+
46+
# Credential is already held by an unused account.
47+
# Take over the unused account.
3248
if existing_credential_holder
33-
if existing_credential_holder == current_user
34-
flash.notice = I18n.t('auth.already_linked', provider: I18n.t("auth.#{provider}"))
35-
return redirect_to edit_user_registration_path
36-
elsif existing_credential_holder.has_activity?
37-
# Linking is not possible and takeover is not possible
38-
# Display a custom error message explaining the credential is already
39-
# tied to an account, and what we can do about it.
40-
flash.alert = I18n.t('auth.already_in_use', provider: I18n.t("auth.#{provider}"))
41-
return redirect_to edit_user_registration_path
42-
else
43-
# The credential is tied to an unused account.
44-
# Destroy the unused account and proceed to link this one.
45-
move_sections_and_destroy_source_user \
46-
source_user: existing_credential_holder,
47-
destination_user: current_user,
48-
takeover_type: 'connect_provider'
49-
end
49+
move_sections_and_destroy_source_user \
50+
source_user: existing_credential_holder,
51+
destination_user: current_user,
52+
takeover_type: 'connect_provider'
5053
end
5154

5255
# TODO: some of this won't work right for non-Google providers, because info comes in differently

0 commit comments

Comments
 (0)