user.ts 4.5 KB

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