From 5ec5a5963c81acd0394f0df417c43b2888c321f2 Mon Sep 17 00:00:00 2001 From: mirovladimitrovski Date: Wed, 20 Nov 2024 11:57:51 +0100 Subject: [PATCH 1/2] fix: do not perform logout if password reset with token --- .../common/src/services/StorageService.ts | 2 +- .../integrations/jwp/JWPAPIService.ts | 2 +- .../AccountModal/forms/EditPassword.tsx | 19 +++++++++---------- .../web/src/services/LocalStorageService.ts | 8 ++++---- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/packages/common/src/services/StorageService.ts b/packages/common/src/services/StorageService.ts index f52f0be26..9333edc18 100644 --- a/packages/common/src/services/StorageService.ts +++ b/packages/common/src/services/StorageService.ts @@ -5,7 +5,7 @@ export default abstract class StorageService { abstract setItem(key: string, value: string, usePrefix?: boolean): Promise; - abstract removeItem(key: string): Promise; + abstract removeItem(key: string, usePrefix?: boolean): Promise; abstract base64Decode(input: string): string; diff --git a/packages/common/src/services/integrations/jwp/JWPAPIService.ts b/packages/common/src/services/integrations/jwp/JWPAPIService.ts index 86439a7e0..3ecbcb9e1 100644 --- a/packages/common/src/services/integrations/jwp/JWPAPIService.ts +++ b/packages/common/src/services/integrations/jwp/JWPAPIService.ts @@ -51,7 +51,7 @@ export default class JWPAPIService { }; removeToken = async () => { - await Promise.all([this.storageService.removeItem(INPLAYER_TOKEN_KEY), this.storageService.removeItem(INPLAYER_IOT_KEY)]); + await Promise.all([this.storageService.removeItem(INPLAYER_TOKEN_KEY, false), this.storageService.removeItem(INPLAYER_IOT_KEY, false)]); }; isAuthenticated = async () => { diff --git a/packages/ui-react/src/containers/AccountModal/forms/EditPassword.tsx b/packages/ui-react/src/containers/AccountModal/forms/EditPassword.tsx index 22c54a2ba..1500f95dc 100644 --- a/packages/ui-react/src/containers/AccountModal/forms/EditPassword.tsx +++ b/packages/ui-react/src/containers/AccountModal/forms/EditPassword.tsx @@ -32,24 +32,23 @@ const ResetPassword = ({ type }: { type?: 'add' }) => { return setSubmitting(false); } - let resetToken = resetPasswordTokenParam; - if (resetPasswordToken) { - resetToken = resetPasswordToken; - } + + const resetToken = resetPasswordToken || resetPasswordTokenParam; + try { - if (user && !resetToken) { - await accountController.changePasswordWithOldPassword(oldPassword || '', password, passwordConfirmation); + if (resetToken) { + await accountController.changePasswordWithToken(emailParam || '', password, resetToken, passwordConfirmation); } else { - if (!resetToken) { + if (!user) { setErrors({ form: t('reset.invalid_link') }); - return setSubmitting(false); } - await accountController.changePasswordWithToken(emailParam || '', password, resetToken, passwordConfirmation); + + await accountController.changePasswordWithOldPassword(oldPassword || '', password, passwordConfirmation); + await accountController.logout(); } announce(t('reset.password_reset_success'), 'success'); - await accountController.logout(); navigate(modalURLFromLocation(location, 'login')); } catch (error: unknown) { if (error instanceof Error) { diff --git a/platforms/web/src/services/LocalStorageService.ts b/platforms/web/src/services/LocalStorageService.ts index eee2c5d6a..1e92b3ff3 100644 --- a/platforms/web/src/services/LocalStorageService.ts +++ b/platforms/web/src/services/LocalStorageService.ts @@ -10,8 +10,8 @@ export class LocalStorageService extends StorageService { this.prefix = prefix; } - getStorageKey(key: string) { - return `${this.prefix}.${key}`; + getStorageKey(key: string, usePrefix = true) { + return usePrefix ? `${this.prefix}.${key}` : key; } async getItem(key: string, parse: boolean, usePrefix = true) { @@ -32,9 +32,9 @@ export class LocalStorageService extends StorageService { } } - async removeItem(key: string) { + async removeItem(key: string, usePrefix = true) { try { - window.localStorage.removeItem(this.getStorageKey(key)); + window.localStorage.removeItem(this.getStorageKey(key, usePrefix)); } catch (error: unknown) { logError('LocalStorageService', 'Failed to remove localStorage entry', { error }); } From a349b22322b01d6e1caaeecb0e6d802cf14cd285 Mon Sep 17 00:00:00 2001 From: mirovladimitrovski Date: Wed, 27 Nov 2024 16:34:53 +0100 Subject: [PATCH 2/2] fix: redirect to index page after reset password with old password --- .../src/containers/AccountModal/forms/EditPassword.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/ui-react/src/containers/AccountModal/forms/EditPassword.tsx b/packages/ui-react/src/containers/AccountModal/forms/EditPassword.tsx index 1500f95dc..0b5fb8032 100644 --- a/packages/ui-react/src/containers/AccountModal/forms/EditPassword.tsx +++ b/packages/ui-react/src/containers/AccountModal/forms/EditPassword.tsx @@ -36,6 +36,8 @@ const ResetPassword = ({ type }: { type?: 'add' }) => { const resetToken = resetPasswordToken || resetPasswordTokenParam; try { + let pathname = location.pathname; + if (resetToken) { await accountController.changePasswordWithToken(emailParam || '', password, resetToken, passwordConfirmation); } else { @@ -44,12 +46,14 @@ const ResetPassword = ({ type }: { type?: 'add' }) => { return setSubmitting(false); } + pathname = '/'; + await accountController.changePasswordWithOldPassword(oldPassword || '', password, passwordConfirmation); await accountController.logout(); } announce(t('reset.password_reset_success'), 'success'); - navigate(modalURLFromLocation(location, 'login')); + navigate(modalURLFromLocation({ ...location, pathname }, 'login')); } catch (error: unknown) { if (error instanceof Error) { if (error.message.includes('invalid param password')) {