user.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import {
  2. RecoveryKey,
  3. TwoFactorRecoveryResponse,
  4. TwoFactorSecret,
  5. TwoFactorVerificationResponse,
  6. UserVerificationResponse,
  7. } from "@ente/accounts/types/user";
  8. import { APPS, OTT_CLIENTS } from "@ente/shared/apps/constants";
  9. import { B64EncryptionResult } from "@ente/shared/crypto/types";
  10. import { ApiError, CustomError } from "@ente/shared/error";
  11. import HTTPService from "@ente/shared/network/HTTPService";
  12. import { getEndpoint } from "@ente/shared/network/api";
  13. import { getToken } from "@ente/shared/storage/localStorage/helpers";
  14. import { KeyAttributes } from "@ente/shared/user/types";
  15. import { HttpStatusCode } from "axios";
  16. import { TwoFactorType } from "../constants/twofactor";
  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. throw e;
  60. }
  61. };
  62. export const verifyTwoFactor = async (code: string, sessionID: string) => {
  63. const resp = await HTTPService.post(
  64. `${ENDPOINT}/users/two-factor/verify`,
  65. {
  66. code,
  67. sessionID,
  68. },
  69. null,
  70. );
  71. return resp.data as UserVerificationResponse;
  72. };
  73. export const recoverTwoFactor = async (
  74. sessionID: string,
  75. twoFactorType: TwoFactorType = TwoFactorType.TOTP,
  76. ) => {
  77. const resp = await HTTPService.get(`${ENDPOINT}/users/two-factor/recover`, {
  78. sessionID,
  79. twoFactorType,
  80. });
  81. return resp.data as TwoFactorRecoveryResponse;
  82. };
  83. export const removeTwoFactor = async (
  84. sessionID: string,
  85. secret: string,
  86. twoFactorType: TwoFactorType = TwoFactorType.TOTP,
  87. ) => {
  88. const resp = await HTTPService.post(`${ENDPOINT}/users/two-factor/remove`, {
  89. sessionID,
  90. secret,
  91. twoFactorType,
  92. });
  93. return resp.data as TwoFactorVerificationResponse;
  94. };
  95. export const changeEmail = async (email: string, ott: string) => {
  96. await HTTPService.post(
  97. `${ENDPOINT}/users/change-email`,
  98. {
  99. email,
  100. ott,
  101. },
  102. null,
  103. {
  104. "X-Auth-Token": getToken(),
  105. },
  106. );
  107. };
  108. export const sendOTTForEmailChange = async (email: string) => {
  109. await HTTPService.post(`${ENDPOINT}/users/ott`, {
  110. email,
  111. client: "web",
  112. purpose: "change",
  113. });
  114. };
  115. export const setupTwoFactor = async () => {
  116. const resp = await HTTPService.post(
  117. `${ENDPOINT}/users/two-factor/setup`,
  118. null,
  119. null,
  120. {
  121. "X-Auth-Token": getToken(),
  122. },
  123. );
  124. return resp.data as TwoFactorSecret;
  125. };
  126. export const enableTwoFactor = async (
  127. code: string,
  128. recoveryEncryptedTwoFactorSecret: B64EncryptionResult,
  129. ) => {
  130. await HTTPService.post(
  131. `${ENDPOINT}/users/two-factor/enable`,
  132. {
  133. code,
  134. encryptedTwoFactorSecret:
  135. recoveryEncryptedTwoFactorSecret.encryptedData,
  136. twoFactorSecretDecryptionNonce:
  137. recoveryEncryptedTwoFactorSecret.nonce,
  138. },
  139. null,
  140. {
  141. "X-Auth-Token": getToken(),
  142. },
  143. );
  144. };
  145. export const setRecoveryKey = (token: string, recoveryKey: RecoveryKey) =>
  146. HTTPService.put(`${ENDPOINT}/users/recovery-key`, recoveryKey, null, {
  147. "X-Auth-Token": token,
  148. });
  149. export const disableTwoFactor = async () => {
  150. await HTTPService.post(`${ENDPOINT}/users/two-factor/disable`, null, null, {
  151. "X-Auth-Token": getToken(),
  152. });
  153. };