From cb6d53dd1f1482f3968b71d9225f9f2dcca71815 Mon Sep 17 00:00:00 2001 From: Evan Bowers Date: Thu, 12 Mar 2026 15:16:12 -0700 Subject: [PATCH] fix: fixes for error handling and session token for password change --- frontend/src/cognito/auth.ts | 6 ++++++ .../ChangePassword/ChangePassword.tsx | 18 +++++++++++------- frontend/src/models/auth.ts | 15 ++++++++++++--- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/frontend/src/cognito/auth.ts b/frontend/src/cognito/auth.ts index 231829a7f..1e2c1cb81 100644 --- a/frontend/src/cognito/auth.ts +++ b/frontend/src/cognito/auth.ts @@ -413,6 +413,12 @@ export class AuthService { public async changePassword(existingPassword: string, newPassword: string) { const awsUser = await this.cognitoAuth.currentAuthenticatedUser() + const session = await this.currentCognitoSession() + const refreshToken = session.getRefreshToken() + await new Promise((res, rej) => { + // @ts-ignore + awsUser.refreshSession(refreshToken, (err: any, data: unknown) => (err ? rej(err) : res(data))) + }) await Auth.changePassword(awsUser, existingPassword, newPassword) } diff --git a/frontend/src/components/ChangePassword/ChangePassword.tsx b/frontend/src/components/ChangePassword/ChangePassword.tsx index 34181f680..8ea7823bd 100644 --- a/frontend/src/components/ChangePassword/ChangePassword.tsx +++ b/frontend/src/components/ChangePassword/ChangePassword.tsx @@ -12,25 +12,30 @@ export const ChangePassword = () => { const [password, setPassword] = useState('') const [isValid, setValid] = useState(false) const [saving, setSaving] = useState(false) + const [key, setKey] = useState(0) const { auth } = useDispatch() const history = useHistory() const evaluateCurrentPassword = (e: { target: { value: React.SetStateAction } }) => { setCurrentPassword(e.target.value.toString()) } - const updatePassword = async (event: { preventDefault: () => void }) => { - event.preventDefault() + const updatePassword = async () => { setSaving(true) - await auth.changePassword({ currentPassword: currentPassword, password: password }) + const success = await auth.changePassword({ currentPassword, password }) setSaving(false) - window.location.reload() + if (success) { + setCurrentPassword('') + setPassword('') + setValid(false) + setKey(k => k + 1) + } } return ( <> Change Password - + { title="Save" variant="contained" color="primary" - type="submit" size="small" disabled={!isValid || saving} onClick={updatePassword} @@ -63,7 +67,7 @@ export const ChangePassword = () => { Changing your password will NOT automatically sign you out of other sessions. - The app will reload after your password is updated. You can manually sign out from all sessions below. + You can manually sign out from all sessions below. ), diff --git a/frontend/src/models/auth.ts b/frontend/src/models/auth.ts index cb0b6fe6c..7be8dff39 100644 --- a/frontend/src/models/auth.ts +++ b/frontend/src/models/auth.ts @@ -111,15 +111,24 @@ export default createModel()({ dispatch.ui.set({ errorMessage: 'Login failed.' }) } }, - async changePassword(passwordValues: IPasswordValue, state) { + async changePassword(passwordValues: IPasswordValue, state): Promise { const existingPassword = passwordValues.currentPassword const newPassword = passwordValues.password try { await state.auth.authService?.changePassword(existingPassword, newPassword) + dispatch.ui.set({ successMessage: 'Password changed successfully.' }) return true - } catch (error) { - dispatch.ui.set({ errorMessage: `Change password error: ${error}` }) + } catch (error: any) { + const message = + error.code === 'NotAuthorizedException' + ? 'Current password is incorrect.' + : error.code === 'InvalidPasswordException' + ? error.message || 'New password does not meet the requirements.' + : error.code === 'LimitExceededException' + ? 'Too many attempts. Please try again later.' + : error.message || 'An unexpected error occurred. Please try again.' + dispatch.ui.set({ errorMessage: message }) return false } },