From 94a6d21b88f2ea4f85bacbf75e5e9f50d69befd1 Mon Sep 17 00:00:00 2001 From: samanera <141537607+samanera@users.noreply.github.com> Date: Fri, 3 Nov 2023 22:13:38 -0600 Subject: [PATCH] omniauth: prevent duplicating accounts Closes: codeandomexico/decidim-monterrey#149 --- ...mniauth_registrations_controller_override.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/overrides/controllers/decidim/devise/omniauth_registrations_controller_override.rb b/app/overrides/controllers/decidim/devise/omniauth_registrations_controller_override.rb index 4d8b2dd..982e4b8 100644 --- a/app/overrides/controllers/decidim/devise/omniauth_registrations_controller_override.rb +++ b/app/overrides/controllers/decidim/devise/omniauth_registrations_controller_override.rb @@ -1,6 +1,23 @@ Decidim::Devise::OmniauthRegistrationsController.class_eval do private + def user_params_from_oauth_hash + return nil if oauth_data.empty? + + email = oauth_data.dig(:info, :email) + user = Decidim::User.find_by(email: email, organization: current_organization) + + { + provider: oauth_data[:provider], + uid: oauth_data[:uid], + name: oauth_data[:info][:name] || user&.name, + nickname: oauth_data[:info][:nickname] || user&.nickname, + oauth_signature: Decidim::OmniauthRegistrationForm.create_signature(oauth_data[:provider], oauth_data[:uid]), + avatar_url: oauth_data[:info][:image], + raw_data: oauth_hash + } + end + def verified_email @verified_email ||= oauth_data.dig(:info, :email) || params.dig(:user, :email) end