mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-14 02:29:35 +00:00
Added the account recovery flow
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
interface Props {
|
||||
email: string;
|
||||
code: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the second part of the account recovery step (a user needs to verify their email).
|
||||
* A user need to click on a button in a magic link page
|
||||
* @param {object} obj
|
||||
* @param {object} obj.email - email of a user that is trying to recover access to their account
|
||||
* @param {object} obj.code - token that a use received via the magic link
|
||||
* @returns
|
||||
*/
|
||||
const EmailVerifyOnPasswordReset = async ({ email, code }: Props) => {
|
||||
const response = await fetch('/api/v1/password/email/password-reset-verify', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
email: email,
|
||||
code: code
|
||||
})
|
||||
});
|
||||
if (response?.status === 200) {
|
||||
return response;
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
'Something went wrong during email verification on password reset.'
|
||||
);
|
||||
};
|
||||
|
||||
export default EmailVerifyOnPasswordReset;
|
||||
@@ -0,0 +1,33 @@
|
||||
interface Props {
|
||||
email: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the first of the account recovery step (a user needs to verify their email).
|
||||
* It will send an email containing a magic link to start the account recovery flow.
|
||||
* @param {object} obj
|
||||
* @param {object} obj.email - email of a user that is trying to recover access to their account
|
||||
* @returns
|
||||
*/
|
||||
const SendEmailOnPasswordReset = async ({ email }: Props) => {
|
||||
const response = await fetch('/api/v1/password/email/password-reset', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
email: email
|
||||
})
|
||||
});
|
||||
// need precise error handling about the status code
|
||||
if (response?.status === 200) {
|
||||
const data = await response.json();
|
||||
return data;
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
'Something went wrong while sending the email verification for password reset.'
|
||||
);
|
||||
};
|
||||
|
||||
export default SendEmailOnPasswordReset;
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* This is the route that get an encrypted private key (will be decrypted with a backup key)
|
||||
* @param {object} obj
|
||||
* @param {object} obj.verificationToken - this is the token that confirms that a user is the right one
|
||||
* @returns
|
||||
*/
|
||||
const getBackupEncryptedPrivateKey = ({
|
||||
verificationToken
|
||||
}: {
|
||||
verificationToken: string;
|
||||
}) => {
|
||||
return fetch('/api/v1/password/backup-private-key', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: 'Bearer ' + verificationToken
|
||||
}
|
||||
}).then(async (res) => {
|
||||
if (res?.status !== 200) {
|
||||
console.log('Failed to get the backup key');
|
||||
}
|
||||
return (await res?.json())?.backupPrivateKey;
|
||||
});
|
||||
};
|
||||
|
||||
export default getBackupEncryptedPrivateKey;
|
||||
@@ -0,0 +1,50 @@
|
||||
interface Props {
|
||||
verificationToken: string;
|
||||
encryptedPrivateKey: string;
|
||||
iv: string;
|
||||
tag: string;
|
||||
salt: string;
|
||||
verifier: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the route that resets the account password if all the previus steps were passed
|
||||
* @param {object} obj
|
||||
* @param {object} obj.verificationToken - this is the token that confirms that a user is the right one
|
||||
* @param {object} obj.encryptedPrivateKey - the new encrypted private key (encrypted using the new password)
|
||||
* @param {object} obj.iv
|
||||
* @param {object} obj.tag
|
||||
* @param {object} obj.salt
|
||||
* @param {object} obj.verifier
|
||||
* @returns
|
||||
*/
|
||||
const resetPasswordOnAccountRecovery = ({
|
||||
verificationToken,
|
||||
encryptedPrivateKey,
|
||||
iv,
|
||||
tag,
|
||||
salt,
|
||||
verifier
|
||||
}: Props) => {
|
||||
return fetch('/api/v1/password/password-reset', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: 'Bearer ' + verificationToken
|
||||
},
|
||||
body: JSON.stringify({
|
||||
encryptedPrivateKey: encryptedPrivateKey,
|
||||
iv: iv,
|
||||
tag: tag,
|
||||
salt: salt,
|
||||
verifier: verifier
|
||||
})
|
||||
}).then(async (res) => {
|
||||
if (res?.status !== 200) {
|
||||
console.log('Failed to get the backup key');
|
||||
}
|
||||
return res;
|
||||
});
|
||||
};
|
||||
|
||||
export default resetPasswordOnAccountRecovery;
|
||||
Reference in New Issue
Block a user