Skip to content

Commit

Permalink
Merge pull request #127 from hooktstudios/bugfix/resend-unconfirmed-e…
Browse files Browse the repository at this point in the history
…mail

Allow resend of confirmation with unconfirmed email
  • Loading branch information
00dav00 committed Aug 15, 2020
2 parents 0c23e4e + 1e33728 commit 1549099
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
2 changes: 0 additions & 2 deletions app/models/graphql_devise/concerns/model.rb
Expand Up @@ -11,8 +11,6 @@ def update_with_email(attributes = {})
GraphqlDevise::Model::WithEmailUpdater.new(self, attributes).call
end

private

def pending_reconfirmation?
devise_modules.include?(:confirmable) && try(:unconfirmed_email).present?
end
Expand Down
18 changes: 13 additions & 5 deletions lib/graphql_devise/mutations/resend_confirmation.rb
Expand Up @@ -9,15 +9,14 @@ class ResendConfirmation < Base
field :message, String, null: false

def resolve(email:, redirect_url:)
resource = find_resource(
:email,
get_case_insensitive_field(:email, email)
)
resource = find_confirmable_resource(email)

if resource
yield resource if block_given?

raise_user_error(I18n.t('graphql_devise.confirmations.already_confirmed')) if resource.confirmed?
if resource.confirmed? && !resource.pending_reconfirmation?
raise_user_error(I18n.t('graphql_devise.confirmations.already_confirmed'))
end

resource.send_confirmation_instructions(
redirect_url: redirect_url,
Expand All @@ -30,6 +29,15 @@ def resolve(email:, redirect_url:)
raise_user_error(I18n.t('graphql_devise.confirmations.user_not_found', email: email))
end
end

private

def find_confirmable_resource(email)
email_insensitive = get_case_insensitive_field(:email, email)
resource = find_resource(:unconfirmed_email, email_insensitive) if resource_class.reconfirmable
resource ||= find_resource(:email, email_insensitive)
resource
end
end
end
end
23 changes: 23 additions & 0 deletions spec/requests/mutations/resend_confirmation_spec.rb
Expand Up @@ -98,6 +98,29 @@
end
end

context 'when the email was changed' do
let(:email) { 'new-email@wallaceinc.com' }
let(:new_email) { email }

before do
user.class.reconfirmable = true
user.confirm
user.update_with_email(
email: new_email,
schema_url: 'http://localhost/test',
confirmation_success_url: 'https://google.com'
)
end

it 'sends new confirmation email' do
expect { post_request }.to change(ActionMailer::Base.deliveries, :count).by(1)
expect(ActionMailer::Base.deliveries.first.to).to contain_exactly(new_email)
expect(json_response[:data][:userResendConfirmation]).to include(
message: 'You will receive an email with instructions for how to confirm your email address in a few minutes.'
)
end
end

context "when the email isn't in the system" do
let(:email) { 'nothere@gmail.com' }

Expand Down

0 comments on commit 1549099

Please sign in to comment.