diff --git a/frontend/src/components/utilities/cryptography/changePassword.ts b/frontend/src/components/utilities/cryptography/changePassword.ts index bdc741b3d..aeb138ec6 100644 --- a/frontend/src/components/utilities/cryptography/changePassword.ts +++ b/frontend/src/components/utilities/cryptography/changePassword.ts @@ -13,8 +13,6 @@ import { deriveArgonKey } from './crypto'; const clientOldPassword = new jsrp.client(); const clientNewPassword = new jsrp.client(); -// TODO: modify this function - /** * This function loggs in the user (whether it's right after signup, or a normal login) * @param {*} email @@ -105,9 +103,8 @@ const changePassword = async ( secret: Buffer.from(derivedKey.hash) }); - let res; try { - res = await changePassword2({ + await changePassword2({ clientProof, protectedKey, protectedKeyIV, @@ -118,23 +115,19 @@ const changePassword = async ( salt: result.salt, verifier: result.verifier }); - + saveTokenToLocalStorage({ protectedKey, protectedKeyIV, protectedKeyTag, encryptedPrivateKey, iv: encryptedPrivateKeyIV, - tag: encryptedPrivateKeyTag, + tag: encryptedPrivateKeyTag }); - if (res && res.status === 400) { - setCurrentPasswordError(true); - } else if (res && res.status === 200) { - setPasswordChanged(true); - setCurrentPassword(''); - setNewPassword(''); - } + setPasswordChanged(true); + setCurrentPassword(''); + setNewPassword(''); } catch (error) { setCurrentPasswordError(true); console.log(error); diff --git a/frontend/src/components/utilities/saveTokenToLocalStorage.ts b/frontend/src/components/utilities/saveTokenToLocalStorage.ts index 90cf5bd7d..c47241ed0 100644 --- a/frontend/src/components/utilities/saveTokenToLocalStorage.ts +++ b/frontend/src/components/utilities/saveTokenToLocalStorage.ts @@ -3,9 +3,9 @@ interface Props { protectedKeyIV?: string; protectedKeyTag?: string; publicKey?: string; - encryptedPrivateKey: string; - iv: string; - tag: string; + encryptedPrivateKey?: string; + iv?: string; + tag?: string; privateKey?: string; } @@ -20,38 +20,46 @@ export const saveTokenToLocalStorage = ({ privateKey, }: Props) => { try { - localStorage.removeItem("protectedKey"); - localStorage.removeItem("protectedKeyIV"); - localStorage.removeItem("protectedKeyTag"); - localStorage.removeItem("publicKey"); - localStorage.removeItem("encryptedPrivateKey"); - localStorage.removeItem("iv"); - localStorage.removeItem("tag"); - localStorage.removeItem("PRIVATE_KEY"); if (protectedKey) { + localStorage.removeItem("protectedKey"); localStorage.setItem("protectedKey", protectedKey); } if (protectedKeyIV) { + localStorage.removeItem("protectedKeyIV"); localStorage.setItem("protectedKeyIV", protectedKeyIV); } if (protectedKeyTag) { + localStorage.removeItem("protectedKeyTag"); localStorage.setItem("protectedKeyTag", protectedKeyTag); } if (publicKey) { + localStorage.removeItem("publicKey"); localStorage.setItem("publicKey", publicKey); } - if (privateKey) { - localStorage.setItem("PRIVATE_KEY", privateKey); + if (encryptedPrivateKey) { + localStorage.removeItem("encryptedPrivateKey"); + localStorage.setItem("encryptedPrivateKey", encryptedPrivateKey); } - localStorage.setItem("encryptedPrivateKey", encryptedPrivateKey); - localStorage.setItem("iv", iv); - localStorage.setItem("tag", tag); + if (iv) { + localStorage.removeItem("iv"); + localStorage.setItem("iv", iv); + } + + if (tag) { + localStorage.removeItem("tag"); + localStorage.setItem("tag", tag); + } + + if (privateKey) { + localStorage.removeItem("PRIVATE_KEY"); + localStorage.setItem("PRIVATE_KEY", privateKey); + } } catch (err) { if (err instanceof Error) { throw new Error( diff --git a/frontend/src/pages/api/auth/ChangePassword2.ts b/frontend/src/pages/api/auth/ChangePassword2.ts index b2ab4906e..1094af814 100644 --- a/frontend/src/pages/api/auth/ChangePassword2.ts +++ b/frontend/src/pages/api/auth/ChangePassword2.ts @@ -1,4 +1,4 @@ -import SecurityClient from '@app/components/utilities/SecurityClient'; +import { apiRequest } from "@app/config/request"; interface Props { clientProof: string; @@ -17,7 +17,7 @@ interface Props { * @param {*} clientPublicKey * @returns */ -const changePassword2 = ({ +const changePassword2 = async ({ clientProof, protectedKey, protectedKeyIV, @@ -27,29 +27,20 @@ const changePassword2 = ({ encryptedPrivateKeyTag, salt, verifier -}: Props) => - SecurityClient.fetchCall('/api/v1/password/change-password', { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - clientProof, - protectedKey, - protectedKeyIV, - protectedKeyTag, - encryptedPrivateKey, - encryptedPrivateKeyIV, - encryptedPrivateKeyTag, - salt, - verifier - }) - }).then(async (res) => { - if (res && res.status === 200) { - return res; - } - console.log('Failed to change the password'); - return undefined; +}: Props) => { + const { data } = await apiRequest.post('/api/v1/password/change-password', { + clientProof, + protectedKey, + protectedKeyIV, + protectedKeyTag, + encryptedPrivateKey, + encryptedPrivateKeyIV, + encryptedPrivateKeyTag, + salt, + verifier }); + + return data; +} export default changePassword2;