-
Notifications
You must be signed in to change notification settings - Fork 45
Check reset password token mutation #10
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 all commits
Commits
Show all changes
2 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
52 changes: 52 additions & 0 deletions
52
app/graphql/graphql_devise/mutations/check_password_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,52 @@ | ||
| module GraphqlDevise | ||
| module Mutations | ||
| class CheckPasswordToken < Base | ||
| argument :reset_password_token, String, required: true | ||
| argument :redirect_url, String, required: false | ||
|
|
||
| def resolve(reset_password_token:, redirect_url: nil) | ||
| 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? | ||
|
|
||
| if resource.reset_password_period_valid? | ||
| token_info = client_and_token(resource.create_token) | ||
|
|
||
| resource.skip_confirmation! if confirmable_enabled? && !resource.confirmed_at | ||
| resource.allow_password_change = true if recoverable_enabled? | ||
|
|
||
| resource.save! | ||
|
|
||
| yield resource if block_given? | ||
|
|
||
| redirect_header_options = { reset_password: true } | ||
| redirect_headers = controller.send( | ||
| :build_redirect_headers, | ||
| token_info.fetch(:token), | ||
| token_info.fetch(:client_id), | ||
| redirect_header_options | ||
| ) | ||
|
|
||
| if redirect_url.present? | ||
| controller.redirect_to(resource.build_auth_url(redirect_url, redirect_headers)) | ||
| else | ||
| set_auth_headers(resource) | ||
| end | ||
|
|
||
| { authenticable: resource } | ||
| else | ||
| raise_user_error(I18n.t('graphql_devise.passwords.reset_token_expired')) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def client_and_token(token) | ||
| if Gem::Version.new(DeviseTokenAuth::VERSION) <= Gem::Version.new('1.1.0') | ||
| { client_id: token.first, token: token.last } | ||
| else | ||
| { client_id: token.client, token: token.token } | ||
| end | ||
| 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'Check Password Token Requests' do | ||
| include_context 'with graphql query request' | ||
|
|
||
| let(:user) { create(:user, :confirmed) } | ||
| let(:redirect_url) { 'https://google.com' } | ||
| let(:query) do | ||
| <<-GRAPHQL | ||
| mutation { | ||
| userCheckPasswordToken( | ||
| resetPasswordToken: "#{token}", | ||
| redirectUrl: "#{redirect_url}" | ||
| ) { | ||
| authenticable { email } | ||
| } | ||
| } | ||
| GRAPHQL | ||
| end | ||
|
|
||
| context 'when reset password token is valid' do | ||
| let(:token) { user.send(:set_reset_password_token) } | ||
|
|
||
| context 'when redirect_url is not provided' do | ||
| let(:redirect_url) { nil } | ||
|
|
||
| it 'returns authenticable and credentials in the headers' do | ||
| get_request | ||
|
|
||
| expect(response).to include_auth_headers | ||
| expect(json_response[:data][:userCheckPasswordToken]).to match( | ||
| authenticable: { email: user.email } | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| context 'when redirect url is provided' do | ||
| it 'redirects to redirect url' do | ||
| expect do | ||
| get_request | ||
|
|
||
| user.reload | ||
| end.to change { user.tokens.keys.count }.from(0).to(1).and( | ||
| change(user, :allow_password_change).from(false).to(true) | ||
| ) | ||
|
|
||
| expect(response).to redirect_to %r{\Ahttps://google.com} | ||
| expect(response.body).to include("client=#{user.reload.tokens.keys.first}") | ||
| expect(response.body).to include('access-token=') | ||
| expect(response.body).to include('uid=') | ||
| expect(response.body).to include('expiry=') | ||
| end | ||
| end | ||
|
|
||
| context 'when token has expired' do | ||
| it 'returns an expired token error' do | ||
| travel_to 10.hours.ago do | ||
| token | ||
| end | ||
|
|
||
| get_request | ||
|
|
||
| expect(json_response[:errors]).to contain_exactly( | ||
| hash_including(message: 'Reset password token is no longer valid.', extensions: { code: 'USER_ERROR' }) | ||
| ) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context 'when reset password token is not found' do | ||
| let(:token) { user.send(:set_reset_password_token) + 'invalid' } | ||
|
|
||
| it 'redirects to redirect url' do | ||
| get_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 |
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.
Perhaps we can create a method for this check, It is also used in
clientmethod on the base mutation