-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix!(omniauth): keep verified email in session between registration f…
…orm when needed
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
lib/extends/controllers/decidim/devise/omniauth_registrations_controller_extends.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
module OmniauthRegistrationsControllerExtends | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
def create | ||
form_params = user_params_from_oauth_hash || params[:user] | ||
|
||
@form = form(Decidim::OmniauthRegistrationForm).from_params(form_params) | ||
@form.email ||= verified_email | ||
|
||
Decidim::CreateOmniauthRegistration.call(@form, verified_email) do | ||
on(:ok) do |user| | ||
if user.active_for_authentication? | ||
sign_in_and_redirect user, event: :authentication | ||
set_flash_message :notice, :success, kind: @form.provider.capitalize | ||
else | ||
expire_data_after_sign_in! | ||
user.resend_confirmation_instructions unless user.confirmed? | ||
redirect_to decidim.root_path | ||
flash[:notice] = t("devise.registrations.signed_up_but_unconfirmed") | ||
end | ||
end | ||
|
||
on(:invalid) do | ||
set_flash_message :notice, :success, kind: @form.provider.capitalize | ||
session["devise.omniauth.verified_email"] = verified_email | ||
render :new | ||
end | ||
|
||
on(:error) do |user| | ||
set_flash_message :alert, :failure, kind: @form.provider.capitalize, reason: t("decidim.devise.omniauth_registrations.create.email_already_exists") if user.errors[:email] | ||
session["devise.omniauth.verified_email"] = verified_email | ||
render :new | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def verified_email | ||
@verified_email ||= oauth_data.dig(:info, :email) || session.delete("devise.omniauth.verified_email") | ||
end | ||
end | ||
end | ||
|
||
Decidim::Devise::OmniauthRegistrationsController.class_eval do | ||
include(OmniauthRegistrationsControllerExtends) | ||
end |