|
| 1 | +module GraphqlDevise |
| 2 | + module Mutations |
| 3 | + class SignUp < Base |
| 4 | + argument :email, String, required: true |
| 5 | + argument :name, String, required: false |
| 6 | + argument :password, String, required: true |
| 7 | + argument :password_confirmation, String, required: true |
| 8 | + argument :confirm_success_url, String, required: false |
| 9 | + argument :config_name, String, required: false |
| 10 | + |
| 11 | + field :authenticable, GraphqlDevise::Types::AuthenticableType, null: true |
| 12 | + field :success, Boolean, null: false |
| 13 | + field :errors, [String], null: false |
| 14 | + |
| 15 | + def resolve(email:, **attrs) |
| 16 | + redirect_url = attrs.delete(:confirm_success_url) |
| 17 | + resource = resource_class.new(email: email, provider: :email, **attrs) |
| 18 | + |
| 19 | + if resource.present? |
| 20 | + resource.skip_confirmation_notification! if resource.respond_to?(:skip_confirmation_notification!) |
| 21 | + |
| 22 | + if resource.save |
| 23 | + yield resource if block_given? |
| 24 | + |
| 25 | + if confirmable_enabled?(resource) && !resource.confirmed? |
| 26 | + # user will require email authentication |
| 27 | + resource.send_confirmation_instructions( |
| 28 | + client_config: attrs[:config_name], |
| 29 | + redirect_url: redirect_url |
| 30 | + ) |
| 31 | + end |
| 32 | + |
| 33 | + set_auth_headers(resource) if active_for_authentication?(resource) |
| 34 | + |
| 35 | + { success: true, authenticable: resource, errors: [] } |
| 36 | + else |
| 37 | + clean_up_passwords(resource) |
| 38 | + resource_errors(resource) |
| 39 | + end |
| 40 | + else |
| 41 | + single_error_object("Resource couldn't be built, execution stopped.") |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + protected |
| 46 | + |
| 47 | + def confirmable_enabled?(resource) |
| 48 | + resource.respond_to?(:confirmed_at) |
| 49 | + end |
| 50 | + |
| 51 | + def active_for_authentication?(resource) |
| 52 | + resource.active_for_authentication? |
| 53 | + end |
| 54 | + |
| 55 | + def provider |
| 56 | + :email |
| 57 | + end |
| 58 | + |
| 59 | + # NOTE: Devise controller method, find a way to re use it |
| 60 | + def clean_up_passwords(resource) |
| 61 | + resource.clean_up_passwords if object.respond_to?(:clean_up_passwords) |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | +end |
0 commit comments