|
| 1 | +module GraphqlDevise |
| 2 | + module Mutations |
| 3 | + class UpdatePassword < Base |
| 4 | + argument :password, String, required: true |
| 5 | + argument :password_confirmation, String, required: true |
| 6 | + argument :current_password, String, required: false |
| 7 | + |
| 8 | + def resolve(current_password: nil, **attrs) |
| 9 | + if current_resource.blank? |
| 10 | + raise_user_error(I18n.t('graphql_devise.not_authenticated')) |
| 11 | + elsif current_resource.provider != 'email' |
| 12 | + raise_user_error( |
| 13 | + I18n.t('graphql_devise.passwords.password_not_required', provider: current_resource.provider.humanize) |
| 14 | + ) |
| 15 | + end |
| 16 | + |
| 17 | + if update_cresource_password(current_password, attrs) |
| 18 | + current_resource.allow_password_change = false if recoverable_enabled? |
| 19 | + current_resource.save! |
| 20 | + |
| 21 | + yield current_resource if block_given? |
| 22 | + |
| 23 | + { authenticable: current_resource } |
| 24 | + else |
| 25 | + raise_user_error_list( |
| 26 | + I18n.t('graphql_devise.passwords.update_password_error'), |
| 27 | + errors: current_resource.errors.full_messages |
| 28 | + ) |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + private |
| 33 | + |
| 34 | + def update_cresource_password(current_password, attrs) |
| 35 | + allow_password_change = recoverable_enabled? && current_resource.allow_password_change == true |
| 36 | + if DeviseTokenAuth.check_current_password_before_update == false || allow_password_change |
| 37 | + current_resource.public_send(:update, attrs) |
| 38 | + else |
| 39 | + current_resource.public_send(:update_with_password, attrs.merge(current_password: current_password)) |
| 40 | + end |
| 41 | + end |
| 42 | + end |
| 43 | + end |
| 44 | +end |
0 commit comments