ente/web/packages/accounts/api/user.ts

171 lines
4.7 KiB
TypeScript
Raw Normal View History

import HTTPService from "@ente/shared/network/HTTPService";
import { getEndpoint } from "@ente/shared/network/api";
2023-11-02 05:26:51 +00:00
2023-11-02 11:48:39 +00:00
import {
RecoveryKey,
2023-11-02 11:48:39 +00:00
TwoFactorRecoveryResponse,
2023-11-02 12:21:58 +00:00
TwoFactorSecret,
TwoFactorVerificationResponse,
UserVerificationResponse,
} from "@ente/accounts/types/user";
import { APPS, OTT_CLIENTS } from "@ente/shared/apps/constants";
import { B64EncryptionResult } from "@ente/shared/crypto/types";
import { ApiError, CustomError } from "@ente/shared/error";
import { logError } from "@ente/shared/sentry";
import { getToken } from "@ente/shared/storage/localStorage/helpers";
import { KeyAttributes } from "@ente/shared/user/types";
import { HttpStatusCode } from "axios";
2024-03-11 17:30:11 +00:00
import { TwoFactorType } from "../constants/twofactor";
2023-11-02 05:26:51 +00:00
const ENDPOINT = getEndpoint();
2023-11-10 09:38:10 +00:00
export const sendOtt = (appName: APPS, email: string) => {
2023-11-02 05:26:51 +00:00
return HTTPService.post(`${ENDPOINT}/users/ott`, {
email,
2023-11-10 09:38:10 +00:00
client: OTT_CLIENTS.get(appName),
2023-11-02 05:26:51 +00:00
});
};
export const verifyOtt = (email: string, ott: string, referral: string) => {
const cleanedReferral = `web:${referral?.trim() || ""}`;
return HTTPService.post(`${ENDPOINT}/users/verify-email`, {
email,
ott,
source: cleanedReferral,
});
};
2023-11-02 05:26:51 +00:00
2023-11-10 09:38:10 +00:00
export const putAttributes = (token: string, keyAttributes: KeyAttributes) =>
HTTPService.put(
2023-11-02 05:26:51 +00:00
`${ENDPOINT}/users/attributes`,
{ keyAttributes },
undefined,
{
"X-Auth-Token": token,
},
2023-11-02 05:26:51 +00:00
);
export const _logout = async () => {
try {
2023-11-10 09:38:10 +00:00
const token = getToken();
2023-11-02 05:26:51 +00:00
await HTTPService.post(`${ENDPOINT}/users/logout`, null, undefined, {
"X-Auth-Token": token,
2023-11-02 05:26:51 +00:00
});
} catch (e) {
2023-11-10 09:38:10 +00:00
// ignore if token missing can be triggered during sign up.
if (e instanceof Error && e.message === CustomError.TOKEN_MISSING) {
return;
}
2023-11-02 05:26:51 +00:00
// ignore if unauthorized, can be triggered during on token expiry.
2023-11-10 09:38:10 +00:00
else if (
2023-11-02 05:26:51 +00:00
e instanceof ApiError &&
e.httpStatusCode === HttpStatusCode.Unauthorized
) {
2023-11-10 09:38:10 +00:00
return;
2023-11-02 05:26:51 +00:00
}
logError(e, "/users/logout failed");
2023-11-02 05:26:51 +00:00
throw e;
}
};
2023-11-02 10:39:16 +00:00
export const verifyTwoFactor = async (code: string, sessionID: string) => {
const resp = await HTTPService.post(
`${ENDPOINT}/users/two-factor/verify`,
{
code,
sessionID,
},
null,
2023-11-02 10:39:16 +00:00
);
return resp.data as UserVerificationResponse;
};
2023-11-02 11:48:39 +00:00
2024-03-11 17:30:11 +00:00
export const recoverTwoFactor = async (
sessionID: string,
2024-03-11 17:40:49 +00:00
twoFactorType: TwoFactorType = TwoFactorType.TOTP,
2024-03-11 17:30:11 +00:00
) => {
2023-11-02 11:48:39 +00:00
const resp = await HTTPService.get(`${ENDPOINT}/users/two-factor/recover`, {
sessionID,
2024-03-11 17:30:11 +00:00
twoFactorType,
2023-11-02 11:48:39 +00:00
});
return resp.data as TwoFactorRecoveryResponse;
};
2024-03-11 17:30:11 +00:00
export const removeTwoFactor = async (
sessionID: string,
secret: string,
2024-03-11 17:40:49 +00:00
twoFactorType: TwoFactorType = TwoFactorType.TOTP,
2024-03-11 17:30:11 +00:00
) => {
2023-11-02 11:48:39 +00:00
const resp = await HTTPService.post(`${ENDPOINT}/users/two-factor/remove`, {
sessionID,
secret,
2024-03-11 17:30:11 +00:00
twoFactorType,
2023-11-02 11:48:39 +00:00
});
return resp.data as TwoFactorVerificationResponse;
};
2023-11-02 12:12:29 +00:00
export const changeEmail = async (email: string, ott: string) => {
await HTTPService.post(
`${ENDPOINT}/users/change-email`,
{
email,
ott,
},
null,
{
"X-Auth-Token": getToken(),
},
2023-11-02 12:12:29 +00:00
);
};
export const sendOTTForEmailChange = async (email: string) => {
await HTTPService.post(`${ENDPOINT}/users/ott`, {
email,
client: "web",
purpose: "change",
2023-11-02 12:12:29 +00:00
});
};
2023-11-02 12:21:58 +00:00
export const setupTwoFactor = async () => {
const resp = await HTTPService.post(
`${ENDPOINT}/users/two-factor/setup`,
null,
null,
{
"X-Auth-Token": getToken(),
},
2023-11-02 12:21:58 +00:00
);
return resp.data as TwoFactorSecret;
};
export const enableTwoFactor = async (
code: string,
recoveryEncryptedTwoFactorSecret: B64EncryptionResult,
2023-11-02 12:21:58 +00:00
) => {
await HTTPService.post(
`${ENDPOINT}/users/two-factor/enable`,
{
code,
encryptedTwoFactorSecret:
recoveryEncryptedTwoFactorSecret.encryptedData,
twoFactorSecretDecryptionNonce:
recoveryEncryptedTwoFactorSecret.nonce,
},
null,
{
"X-Auth-Token": getToken(),
},
2023-11-02 12:21:58 +00:00
);
};
export const setRecoveryKey = (token: string, recoveryKey: RecoveryKey) =>
HTTPService.put(`${ENDPOINT}/users/recovery-key`, recoveryKey, null, {
"X-Auth-Token": token,
2023-11-02 12:21:58 +00:00
});
2023-11-02 16:53:14 +00:00
export const disableTwoFactor = async () => {
await HTTPService.post(`${ENDPOINT}/users/two-factor/disable`, null, null, {
"X-Auth-Token": getToken(),
2023-11-02 16:53:14 +00:00
});
};