|
| 1 | +module GraphqlDevise |
| 2 | + module Mutations |
| 3 | + class CheckPasswordToken < Base |
| 4 | + argument :reset_password_token, String, required: true |
| 5 | + argument :redirect_url, String, required: false |
| 6 | + |
| 7 | + def resolve(reset_password_token:, redirect_url: nil) |
| 8 | + resource = resource_class.with_reset_password_token(reset_password_token) |
| 9 | + |
| 10 | + if resource && resource.reset_password_period_valid? |
| 11 | + token_info = client_and_token(resource.create_token) |
| 12 | + |
| 13 | + resource.skip_confirmation! if confirmable_enabled? && !resource.confirmed_at |
| 14 | + resource.allow_password_change = true if recoverable_enabled? |
| 15 | + |
| 16 | + resource.save! |
| 17 | + |
| 18 | + yield resource if block_given? |
| 19 | + |
| 20 | + redirect_header_options = { reset_password: true } |
| 21 | + redirect_headers = controller.send( |
| 22 | + :build_redirect_headers, |
| 23 | + token_info.fetch(:token), |
| 24 | + token_info.fetch(:client_id), |
| 25 | + redirect_header_options |
| 26 | + ) |
| 27 | + |
| 28 | + if redirect_url.present? |
| 29 | + controller.redirect_to(resource.build_auth_url(redirect_url, redirect_headers)) |
| 30 | + else |
| 31 | + set_auth_headers(resource) |
| 32 | + end |
| 33 | + |
| 34 | + { authenticable: resource } |
| 35 | + else |
| 36 | + raise ActionController::RoutingError, 'Not Found' |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + private |
| 41 | + |
| 42 | + def client_and_token(token) |
| 43 | + if Gem::Version.new(DeviseTokenAuth::VERSION) <= Gem::Version.new('1.1.0') |
| 44 | + { client_id: token.first, token: token.last } |
| 45 | + else |
| 46 | + { client_id: token.client, token: token.token } |
| 47 | + end |
| 48 | + end |
| 49 | + end |
| 50 | + end |
| 51 | +end |
0 commit comments