core: add support for changing account email

This commit is contained in:
Abdullah Atta
2023-01-09 15:04:44 +05:00
committed by Abdullah Atta
parent 1b471a2fda
commit b439571e17
2 changed files with 33 additions and 3 deletions

View File

@@ -256,8 +256,12 @@ class Database {
await this.user.logout(false, "Account Deleted"); await this.user.logout(false, "Account Deleted");
break; break;
} }
case "userEmailChanged": {
await this.user.logout(true, "Email changed");
break;
}
case "userPasswordChanged": { case "userPasswordChanged": {
await this.user.logout(true, "Password Changed"); await this.user.logout(true, "Password changed");
break; break;
} }
case "emailConfirmed": { case "emailConfirmed": {

View File

@@ -322,16 +322,42 @@ class UserManager {
} }
} }
async sendVerificationEmail() { async sendVerificationEmail(newEmail) {
let token = await this.tokenManager.getAccessToken(); let token = await this.tokenManager.getAccessToken();
if (!token) return; if (!token) return;
await http.post( await http.post(
`${constants.AUTH_HOST}${ENDPOINTS.verifyUser}`, `${constants.AUTH_HOST}${ENDPOINTS.verifyUser}`,
null, { newEmail },
token token
); );
} }
async changeEmail(newEmail, password, code) {
const token = await this.tokenManager.getAccessToken();
if (!token) return;
const user = await this.getUser();
if (!user) return;
const email = newEmail.toLowerCase();
await http.patch(
`${constants.AUTH_HOST}${ENDPOINTS.patchUser}`,
{
type: "change_email",
new_email: newEmail,
password: await this._storage.hash(password, email),
verification_code: code
},
token
);
await this._storage.deriveCryptoKey(`_uk_@${newEmail}`, {
password,
salt: user.salt
});
}
recoverAccount(email) { recoverAccount(email) {
return http.post(`${constants.AUTH_HOST}${ENDPOINTS.recoverAccount}`, { return http.post(`${constants.AUTH_HOST}${ENDPOINTS.recoverAccount}`, {
email, email,