-
Notifications
You must be signed in to change notification settings - Fork 45
Alternate reset password flow, only 2 steps, no redirect #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
37 changes: 37 additions & 0 deletions
37
lib/graphql_devise/mutations/send_password_reset_with_token.rb
This file contains hidden or 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,37 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module GraphqlDevise | ||
| module Mutations | ||
| class SendPasswordResetWithToken < Base | ||
| argument :email, String, required: true | ||
| argument :redirect_url, String, required: true | ||
|
|
||
| field :message, String, null: false | ||
|
|
||
| def resolve(email:, redirect_url:) | ||
| check_redirect_url_whitelist!(redirect_url) | ||
|
|
||
| resource = find_resource(:email, get_case_insensitive_field(:email, email)) | ||
|
|
||
| if resource | ||
| yield resource if block_given? | ||
|
|
||
| resource.send_reset_password_instructions( | ||
| email: email, | ||
| provider: 'email', | ||
| redirect_url: redirect_url, | ||
| template_path: ['graphql_devise/mailer'] | ||
| ) | ||
|
|
||
| if resource.errors.empty? | ||
| { message: I18n.t('graphql_devise.passwords.send_instructions') } | ||
| else | ||
| raise_user_error_list(I18n.t('graphql_devise.invalid_resource'), errors: resource.errors.full_messages) | ||
| end | ||
| else | ||
| raise_user_error(I18n.t('graphql_devise.user_not_found')) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
38 changes: 38 additions & 0 deletions
38
lib/graphql_devise/mutations/update_password_with_token.rb
This file contains hidden or 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,38 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module GraphqlDevise | ||
| module Mutations | ||
| class UpdatePasswordWithToken < Base | ||
| argument :password, String, required: true | ||
| argument :password_confirmation, String, required: true | ||
| argument :reset_password_token, String, required: true | ||
|
|
||
| field :credentials, | ||
| GraphqlDevise::Types::CredentialType, | ||
| null: true, | ||
| description: 'Authentication credentials. Resource must be signed_in for credentials to be returned.' | ||
|
|
||
| def resolve(reset_password_token:, **attrs) | ||
| raise_user_error(I18n.t('graphql_devise.passwords.password_recovery_disabled')) unless recoverable_enabled? | ||
|
|
||
| resource = resource_class.with_reset_password_token(reset_password_token) | ||
| raise_user_error(I18n.t('graphql_devise.passwords.reset_token_not_found')) if resource.blank? | ||
| raise_user_error(I18n.t('graphql_devise.passwords.reset_token_expired')) unless resource.reset_password_period_valid? | ||
|
|
||
| if resource.update(attrs) | ||
| yield resource if block_given? | ||
|
|
||
| response_payload = { authenticatable: resource } | ||
| response_payload[:credentials] = set_auth_headers(resource) if controller.signed_in?(resource_name) | ||
|
|
||
| response_payload | ||
| else | ||
| raise_user_error_list( | ||
| I18n.t('graphql_devise.passwords.update_password_error'), | ||
| errors: resource.errors.full_messages | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
13 changes: 13 additions & 0 deletions
13
spec/dummy/app/graphql/mutations/reset_admin_password_with_token.rb
This file contains hidden or 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,13 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Mutations | ||
| class ResetAdminPasswordWithToken < GraphqlDevise::Mutations::UpdatePasswordWithToken | ||
| field :authenticatable, Types::AdminType, null: false | ||
|
|
||
| def resolve(reset_password_token:, **attrs) | ||
| super do |admin| | ||
| controller.sign_in(admin) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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
78 changes: 78 additions & 0 deletions
78
spec/requests/mutations/send_password_reset_with_token_spec.rb
This file contains hidden or 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,78 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'Send Password Reset Requests' do | ||
| include_context 'with graphql query request' | ||
|
|
||
| let!(:user) { create(:user, :confirmed, email: 'jwinnfield@wallaceinc.com') } | ||
| let(:email) { user.email } | ||
| let(:redirect_url) { 'https://google.com' } | ||
| let(:query) do | ||
| <<-GRAPHQL | ||
| mutation { | ||
| userSendPasswordResetWithToken( | ||
| email: "#{email}", | ||
| redirectUrl: "#{redirect_url}" | ||
| ) { | ||
| message | ||
| } | ||
| } | ||
| GRAPHQL | ||
| end | ||
|
|
||
| context 'when redirect_url is not whitelisted' do | ||
| let(:redirect_url) { 'https://not-safe.com' } | ||
|
|
||
| it 'returns a not whitelisted redirect url error' do | ||
| expect { post_request }.to not_change(ActionMailer::Base.deliveries, :count) | ||
|
|
||
| expect(json_response[:errors]).to containing_exactly( | ||
| hash_including( | ||
| message: "Redirect to '#{redirect_url}' not allowed.", | ||
| extensions: { code: 'USER_ERROR' } | ||
| ) | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| context 'when params are correct' do | ||
| context 'when using the gem schema' do | ||
| it 'sends password reset email' do | ||
| expect { post_request }.to change(ActionMailer::Base.deliveries, :count).by(1) | ||
|
|
||
| expect(json_response[:data][:userSendPasswordResetWithToken]).to include( | ||
| message: 'You will receive an email with instructions on how to reset your password in a few minutes.' | ||
| ) | ||
|
|
||
| email = Nokogiri::HTML(ActionMailer::Base.deliveries.last.body.encoded) | ||
| link = email.css('a').first | ||
|
|
||
| expect(link['href']).to include(redirect_url + '?reset_password_token') | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context 'when email address uses different casing' do | ||
| let(:email) { 'jWinnfield@wallaceinc.com' } | ||
|
|
||
| it 'honors devise configuration for case insensitive fields' do | ||
| expect { post_request }.to change(ActionMailer::Base.deliveries, :count).by(1) | ||
| expect(json_response[:data][:userSendPasswordResetWithToken]).to include( | ||
| message: 'You will receive an email with instructions on how to reset your password in a few minutes.' | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| context 'when user email is not found' do | ||
| let(:email) { 'nothere@gmail.com' } | ||
|
|
||
| before { post_request } | ||
|
|
||
| it 'returns an error' do | ||
| expect(json_response[:errors]).to contain_exactly( | ||
| hash_including(message: 'User was not found or was not logged in.', extensions: { code: 'USER_ERROR' }) | ||
| ) | ||
| end | ||
| end | ||
| end |
117 changes: 117 additions & 0 deletions
117
spec/requests/mutations/update_password_with_token_spec.rb
This file contains hidden or 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,117 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'Update Password With Token' do | ||
| include_context 'with graphql query request' | ||
|
|
||
| let(:password) { '12345678' } | ||
| let(:password_confirmation) { password } | ||
|
|
||
| context 'when using the user model' do | ||
| let(:user) { create(:user, :confirmed) } | ||
| let(:query) do | ||
| <<-GRAPHQL | ||
| mutation { | ||
| userUpdatePasswordWithToken( | ||
| resetPasswordToken: "#{token}", | ||
| password: "#{password}", | ||
| passwordConfirmation: "#{password_confirmation}" | ||
| ) { | ||
| authenticatable { email } | ||
| credentials { accessToken } | ||
| } | ||
| } | ||
| GRAPHQL | ||
| end | ||
|
|
||
| context 'when reset password token is valid' do | ||
| let(:token) { user.send(:set_reset_password_token) } | ||
|
|
||
| it 'updates the password' do | ||
| expect do | ||
| post_request | ||
| user.reload | ||
| end.to change(user, :encrypted_password) | ||
|
|
||
| expect(json_response[:data][:userUpdatePasswordWithToken][:credentials]).to be_nil | ||
| expect(json_response[:data][:userUpdatePasswordWithToken][:authenticatable]).to include(email: user.email) | ||
| end | ||
|
|
||
| context 'when token has expired' do | ||
| it 'returns an expired token error' do | ||
| travel_to 10.hours.ago do | ||
| token | ||
| end | ||
|
|
||
| post_request | ||
|
|
||
| expect(json_response[:errors]).to contain_exactly( | ||
| hash_including(message: 'Reset password token is no longer valid.', extensions: { code: 'USER_ERROR' }) | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| context 'when password confirmation does not match' do | ||
| let(:password_confirmation) { 'does not match' } | ||
|
|
||
| it 'returns an error' do | ||
| post_request | ||
|
|
||
| expect(json_response[:errors]).to contain_exactly( | ||
| hash_including( | ||
| message: 'Unable to update user password', | ||
| extensions: { code: 'USER_ERROR', detailed_errors: ["Password confirmation doesn't match Password"] } | ||
| ) | ||
| ) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context 'when reset password token is not found' do | ||
| let(:token) { user.send(:set_reset_password_token) + 'invalid' } | ||
|
|
||
| it 'returns an error' do | ||
| post_request | ||
|
|
||
| expect(json_response[:errors]).to contain_exactly( | ||
| hash_including(message: 'No user found for the specified reset token.', extensions: { code: 'USER_ERROR' }) | ||
| ) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context 'when using the admin model' do | ||
| let(:admin) { create(:admin, :confirmed) } | ||
| let(:query) do | ||
| <<-GRAPHQL | ||
| mutation { | ||
| adminUpdatePasswordWithToken( | ||
| resetPasswordToken: "#{token}", | ||
| password: "#{password}", | ||
| passwordConfirmation: "#{password_confirmation}" | ||
| ) { | ||
| authenticatable { email } | ||
| credentials { uid } | ||
| } | ||
| } | ||
| GRAPHQL | ||
| end | ||
|
|
||
| context 'when reset password token is valid' do | ||
| let(:token) { admin.send(:set_reset_password_token) } | ||
|
|
||
| it 'updates the password' do | ||
| expect do | ||
| post_request | ||
| admin.reload | ||
| end.to change(admin, :encrypted_password) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WDYT about checking the password is valid instead of |
||
|
|
||
| expect(json_response[:data][:adminUpdatePasswordWithToken]).to include( | ||
| credentials: { uid: admin.email }, | ||
| authenticatable: { email: admin.email } | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What kind of black can be passed here? Is there any examples of this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any block you want, all mutations do the same. Check the specs in this PR for an example.