user.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import HTTPService from "@ente/shared/network/HTTPService";
  2. import { getEndpoint } from "@ente/shared/network/api";
  3. import {
  4. RecoveryKey,
  5. TwoFactorRecoveryResponse,
  6. TwoFactorSecret,
  7. TwoFactorVerificationResponse,
  8. UserVerificationResponse,
  9. } from "@ente/accounts/types/user";
  10. import { APPS, OTT_CLIENTS } from "@ente/shared/apps/constants";
  11. import { B64EncryptionResult } from "@ente/shared/crypto/types";
  12. import { ApiError, CustomError } from "@ente/shared/error";
  13. import { logError } from "@ente/shared/sentry";
  14. import { getToken } from "@ente/shared/storage/localStorage/helpers";
  15. import { KeyAttributes } from "@ente/shared/user/types";
  16. import { HttpStatusCode } from "axios";
  17. import { TwoFactorType } from "../constants/twofactor";
  18. const ENDPOINT = getEndpoint();
  19. export const sendOtt = (appName: APPS, email: string) => {
  20. return HTTPService.post(`${ENDPOINT}/users/ott`, {
  21. email,
  22. client: OTT_CLIENTS.get(appName),
  23. });
  24. };
  25. export const verifyOtt = (email: string, ott: string, referral: string) => {
  26. const cleanedReferral = `web:${referral?.trim() || ""}`;
  27. return HTTPService.post(`${ENDPOINT}/users/verify-email`, {
  28. email,
  29. ott,
  30. source: cleanedReferral,
  31. });
  32. };
  33. export const putAttributes = (token: string, keyAttributes: KeyAttributes) =>
  34. HTTPService.put(
  35. `${ENDPOINT}/users/attributes`,
  36. { keyAttributes },
  37. undefined,
  38. {
  39. "X-Auth-Token": token,
  40. },
  41. );
  42. export const _logout = async () => {
  43. try {
  44. const token = getToken();
  45. await HTTPService.post(`${ENDPOINT}/users/logout`, null, undefined, {
  46. "X-Auth-Token": token,
  47. });
  48. } catch (e) {
  49. // ignore if token missing can be triggered during sign up.
  50. if (e instanceof Error && e.message === CustomError.TOKEN_MISSING) {
  51. return;
  52. }
  53. // ignore if unauthorized, can be triggered during on token expiry.
  54. else if (
  55. e instanceof ApiError &&
  56. e.httpStatusCode === HttpStatusCode.Unauthorized
  57. ) {
  58. return;
  59. }
  60. logError(e, "/users/logout failed");
  61. throw e;
  62. }
  63. };
  64. export const verifyTwoFactor = async (code: string, sessionID: string) => {
  65. const resp = await HTTPService.post(
  66. `${ENDPOINT}/users/two-factor/verify`,
  67. {
  68. code,
  69. sessionID,
  70. },
  71. null,
  72. );
  73. return resp.data as UserVerificationResponse;
  74. };
  75. export const recoverTwoFactor = async (
  76. sessionID: string,
  77. twoFactorType: TwoFactorType,
  78. ) => {
  79. const resp = await HTTPService.get(`${ENDPOINT}/users/two-factor/recover`, {
  80. sessionID,
  81. twoFactorType,
  82. });
  83. return resp.data as TwoFactorRecoveryResponse;
  84. };
  85. export const removeTwoFactor = async (
  86. sessionID: string,
  87. secret: string,
  88. twoFactorType: TwoFactorType,
  89. ) => {
  90. const resp = await HTTPService.post(`${ENDPOINT}/users/two-factor/remove`, {
  91. sessionID,
  92. secret,
  93. twoFactorType,
  94. });
  95. return resp.data as TwoFactorVerificationResponse;
  96. };
  97. export const changeEmail = async (email: string, ott: string) => {
  98. await HTTPService.post(
  99. `${ENDPOINT}/users/change-email`,
  100. {
  101. email,
  102. ott,
  103. },
  104. null,
  105. {
  106. "X-Auth-Token": getToken(),
  107. },
  108. );
  109. };
  110. export const sendOTTForEmailChange = async (email: string) => {
  111. await HTTPService.post(`${ENDPOINT}/users/ott`, {
  112. email,
  113. client: "web",
  114. purpose: "change",
  115. });
  116. };
  117. export const setupTwoFactor = async () => {
  118. const resp = await HTTPService.post(
  119. `${ENDPOINT}/users/two-factor/setup`,
  120. null,
  121. null,
  122. {
  123. "X-Auth-Token": getToken(),
  124. },
  125. );
  126. return resp.data as TwoFactorSecret;
  127. };
  128. export const enableTwoFactor = async (
  129. code: string,
  130. recoveryEncryptedTwoFactorSecret: B64EncryptionResult,
  131. ) => {
  132. await HTTPService.post(
  133. `${ENDPOINT}/users/two-factor/enable`,
  134. {
  135. code,
  136. encryptedTwoFactorSecret:
  137. recoveryEncryptedTwoFactorSecret.encryptedData,
  138. twoFactorSecretDecryptionNonce:
  139. recoveryEncryptedTwoFactorSecret.nonce,
  140. },
  141. null,
  142. {
  143. "X-Auth-Token": getToken(),
  144. },
  145. );
  146. };
  147. export const setRecoveryKey = (token: string, recoveryKey: RecoveryKey) =>
  148. HTTPService.put(`${ENDPOINT}/users/recovery-key`, recoveryKey, null, {
  149. "X-Auth-Token": token,
  150. });
  151. export const disableTwoFactor = async () => {
  152. await HTTPService.post(`${ENDPOINT}/users/two-factor/disable`, null, null, {
  153. "X-Auth-Token": getToken(),
  154. });
  155. };