|
| 1 | +module GraphqlDevise |
| 2 | + module Resolvers |
| 3 | + class ConfirmAccount < Base |
| 4 | + argument :confirmation_token, String, required: true |
| 5 | + argument :redirect_url, String, required: true |
| 6 | + |
| 7 | + def resolve(confirmation_token:, redirect_url:) |
| 8 | + resource = resource_class.confirm_by_token(confirmation_token) |
| 9 | + |
| 10 | + if resource.errors.empty? |
| 11 | + yield resource if block_given? |
| 12 | + |
| 13 | + redirect_header_options = { account_confirmation_success: true } |
| 14 | + |
| 15 | + redirect_to_link = if controller.signed_in?(resource_name) |
| 16 | + signed_in_resource.build_auth_url( |
| 17 | + redirect_url, |
| 18 | + redirect_headers( |
| 19 | + token_and_client(controller.signed_in_resource.create_token), |
| 20 | + redirect_header_options |
| 21 | + ) |
| 22 | + ) |
| 23 | + else |
| 24 | + DeviseTokenAuth::Url.generate(redirect_url, redirect_header_options) |
| 25 | + end |
| 26 | + |
| 27 | + controller.redirect_to(redirect_to_link) |
| 28 | + { authenticable: resource } |
| 29 | + else |
| 30 | + raise_user_error(I18n.t('graphql_devise.confirmations.invalid_token')) |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + private |
| 35 | + |
| 36 | + def resource_name |
| 37 | + resource_class.to_s.underscore.tr('/', '_') |
| 38 | + end |
| 39 | + |
| 40 | + def redirect_headers(token_info, options) |
| 41 | + controller.send( |
| 42 | + :build_redirect_headers, |
| 43 | + token_info.fetch(:token), |
| 44 | + token_info.fetch(:client_id), |
| 45 | + options |
| 46 | + ) |
| 47 | + end |
| 48 | + |
| 49 | + def client_and_token(token) |
| 50 | + if Gem::Version.new(DeviseTokenAuth::VERSION) <= Gem::Version.new('1.1.0') |
| 51 | + { client_id: token.first, token: token.last } |
| 52 | + else |
| 53 | + { client_id: token.client, token: token.token } |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | +end |
0 commit comments