Summary
Fixes the misleading error message when changing a passcode: entering an incorrect current passcode previously displayed "Current passcode is required to change it." instead of indicating that the current passcode was incorrect.
- Lark bug: https://hola-tech.sg.larksuite.com/record/UVUcr4bq8e8qDFcSlhxlLQzdgmh
- Priority: Medium
- Module: Change Passcode / User Settings
Root Cause
In be/src/modules/users/services/passcode.service.ts (setPasscode), both missing currentPasscode and failed bcrypt.compare were collapsed into a single condition:
const matchesCurrent = !!currentPasscode && (await bcrypt.compare(currentPasscode, user.passcodeHash));
if (!matchesCurrent) {
await this.registerFailure(key);
throw new UnauthorizedException('Current passcode is required to change it.');
}
When a user entered their current passcode but guessed wrongly, matchesCurrent was false, causing the backend to throw 'Current passcode is required to change it.'. The mobile frontend then surfaced this misleading error in the red error banner.
Changes
-
Backend (
be/src/modules/users/services/passcode.service.ts):- Differentiated missing vs. incorrect current passcode:
- If
!currentPasscode: throw401 PASSCODE_REQUIREDwith message"Current passcode is required to change it.". - If
!(await bcrypt.compare(currentPasscode, user.passcodeHash)): register failure against the lockout key and throw401 PASSCODE_INCORRECTwith message"Current passcode is incorrect.".
- If
- Differentiated missing vs. incorrect current passcode:
-
Unit Tests:
-
be/src/modules/users/services/passcode.service.spec.ts: Updated tests to assert the distinct error codes (PASSCODE_REQUIREDvs.PASSCODE_INCORRECT) and message ("Current passcode is incorrect."). -
fe/src/components/passcode-form.test.tsx: Added test verifying the error banner correctly displays"Current passcode is incorrect."when rejected with that message.
-
Verification
- Backend unit tests:
pnpm --prefix be test src/modules/users/services/passcode.service.spec.ts-> 30/30 passed. - Frontend unit tests:
pnpm --prefix fe test src/components/passcode-form.test.tsx-> 23/23 passed. - Frontend type-check & lint:
pnpm --prefix fe type-check(0 errors),npx eslint src/components/passcode-form.test.tsx(0 errors). - Backend build:
pnpm --prefix be build-> succeeded. - Conventional commitlint: passed.