From f681999d502323374320b8408b74cca200b03790 Mon Sep 17 00:00:00 2001
From: Hadi Cheaito <38328371+hadicheaito1@users.noreply.github.com>
Date: Fri, 20 Jan 2023 11:02:30 -0500
Subject: [PATCH] Change password feedback: old password (#4536)

* Change password feedback: old password

* rubo

* eslint
---
 app/assets/locales/en.json                            | 3 ++-
 app/controllers/api/v1/rooms_controller.rb            | 2 +-
 app/controllers/api/v1/users_controller.rb            | 5 ++++-
 app/javascript/hooks/mutations/users/useChangePwd.jsx | 8 ++++++--
 config/application.rb                                 | 4 +++-
 5 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/app/assets/locales/en.json b/app/assets/locales/en.json
index 118fe21d..9141ece7 100644
--- a/app/assets/locales/en.json
+++ b/app/assets/locales/en.json
@@ -367,7 +367,8 @@
       },
       "users": {
         "invalid_invite": "Your invitation token is either invalid or incorrect. Please contact your administrator to receive a new one",
-        "email_exists": "An account under this email already exists. Please try again with another email"
+        "email_exists": "An account under this email already exists. Please try again with another email",
+        "old_password": "The current password you have entered is incorrect"
       },
       "rooms": {
         "room_limit": "Room can not be created due to room limit being reached"
diff --git a/app/controllers/api/v1/rooms_controller.rb b/app/controllers/api/v1/rooms_controller.rb
index 7ad2ebe0..55124df2 100644
--- a/app/controllers/api/v1/rooms_controller.rb
+++ b/app/controllers/api/v1/rooms_controller.rb
@@ -66,7 +66,7 @@ module Api
       # POST /api/v1/rooms.json
       # Creates a room for the specified user if they are allowed to
       def create
-        return render_error status: :bad_request, errors: 'RoomLimitError' unless PermissionsChecker.new(
+        return render_error status: :bad_request, errors: Rails.configuration.custom_error_msgs[:room_limit] unless PermissionsChecker.new(
           permission_names: 'RoomLimit',
           user_id: room_params[:user_id], current_user:, current_provider:
         ).call
diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb
index 8ec79f1f..b66a72e6 100644
--- a/app/controllers/api/v1/users_controller.rb
+++ b/app/controllers/api/v1/users_controller.rb
@@ -111,7 +111,10 @@ module Api
 
         return render_error status: :bad_request if new_password.blank? || old_password.blank?
 
-        return render_error status: :bad_request unless current_user.authenticate old_password
+        unless current_user.authenticate old_password
+          return render_error status: :bad_request,
+                              errors: Rails.configuration.custom_error_msgs[:incorrect_old_password]
+        end
 
         current_user.update! password: new_password
         render_data status: :ok
diff --git a/app/javascript/hooks/mutations/users/useChangePwd.jsx b/app/javascript/hooks/mutations/users/useChangePwd.jsx
index d1e0d630..b21f8b2f 100644
--- a/app/javascript/hooks/mutations/users/useChangePwd.jsx
+++ b/app/javascript/hooks/mutations/users/useChangePwd.jsx
@@ -12,8 +12,12 @@ export default function useChangePwd() {
       onSuccess: () => {
         toast.success(t('toast.success.user.password_updated'));
       },
-      onError: () => {
-        toast.error(t('toast.error.problem_completing_action'));
+      onError: (err) => {
+        if (err.response.data.errors === 'IncorrectOldPassword') {
+          toast.error(t('toast.error.users.old_password'));
+        } else {
+          toast.error(t('toast.error.problem_completing_action'));
+        }
       },
     },
   );
diff --git a/config/application.rb b/config/application.rb
index 3122a3b1..1f19764c 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -29,7 +29,9 @@ module Greenlight
       email_exists: 'EmailAlreadyExists',
       record_invalid: 'RecordInvalid',
       invite_token_invalid: 'InviteInvalid',
-      hcaptcha_invalid: 'HCaptchaInvalid'
+      hcaptcha_invalid: 'HCaptchaInvalid',
+      incorrect_old_password: 'IncorrectOldPassword',
+      room_limit: 'RoomLimitError'
     }
 
     ActiveModelSerializers.config.adapter = :json
-- 
GitLab