|
| 1 | +module GraphqlDevise |
| 2 | + module Mutations |
| 3 | + class SignUp < Base |
| 4 | + argument :email, String, required: true |
| 5 | + argument :password, String, required: true |
| 6 | + argument :password_confirmation, String, required: true |
| 7 | + argument :confirm_success_url, String, required: false |
| 8 | + argument :config_name, String, required: false |
| 9 | + |
| 10 | + def resolve(confirm_success_url: nil, config_name: nil, **attrs) |
| 11 | + resource = resource_class.new(provider: provider, **attrs) |
| 12 | + |
| 13 | + if resource.present? |
| 14 | + resource.skip_confirmation_notification! if resource.respond_to?(:skip_confirmation_notification!) |
| 15 | + |
| 16 | + if resource.save |
| 17 | + yield resource if block_given? |
| 18 | + |
| 19 | + if confirmable_enabled?(resource) && !resource.confirmed? |
| 20 | + # user will require email authentication |
| 21 | + resource.send_confirmation_instructions( |
| 22 | + client_config: config_name, |
| 23 | + redirect_url: confirm_success_url |
| 24 | + ) |
| 25 | + end |
| 26 | + |
| 27 | + set_auth_headers(resource) if active_for_authentication?(resource) |
| 28 | + |
| 29 | + { authenticable: resource } |
| 30 | + else |
| 31 | + clean_up_passwords(resource) |
| 32 | + raise_user_error_list( |
| 33 | + I18n.t('graphql_devise.registration_failed'), |
| 34 | + errors: resource.errors.full_messages |
| 35 | + ) |
| 36 | + end |
| 37 | + else |
| 38 | + raise_user_error(I18n.t('graphql_devise.resource_build_failed')) |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + protected |
| 43 | + |
| 44 | + def confirmable_enabled?(resource) |
| 45 | + resource.respond_to?(:confirmed_at) |
| 46 | + end |
| 47 | + |
| 48 | + def active_for_authentication?(resource) |
| 49 | + resource.active_for_authentication? |
| 50 | + end |
| 51 | + |
| 52 | + def provider |
| 53 | + :email |
| 54 | + end |
| 55 | + |
| 56 | + def clean_up_passwords(resource) |
| 57 | + controller.send(:clean_up_passwords, resource) |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | +end |
0 commit comments