reauth.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /**
  2. * What the delete-account and delete-workshop dialogs ask for before the
  3. * server will go ahead, and the sentences it refuses with. Shared by the
  4. * server check (reauth.server.ts) and the dialogs, which show the refusal in
  5. * the person's own language by looking its key up here. Giving an account
  6. * with no password its first one asks for the same recent sign-in.
  7. *
  8. * - 'password': the account has a password, and must type it.
  9. * - 'totp': no password, but two-factor is on: a code from the app.
  10. * - 'none': neither (Google or passkey only), so the session itself must be
  11. * fresh, see REAUTH_FRESH_SESSION_MINUTES.
  12. */
  13. export type ReauthRequirement = 'password' | 'totp' | 'none'
  14. /** How recent a sign-in must be when there is nothing else to ask for. */
  15. export const REAUTH_FRESH_SESSION_MINUTES = 10
  16. export const REAUTH_ERRORS = {
  17. wrongPassword: 'Wrong password.',
  18. wrongCode: 'Wrong code.',
  19. tooManyAttempts: 'Too many attempts. Wait a few minutes and try again.',
  20. signInAgainAccount: 'Sign in again before deleting your account.',
  21. signInAgainWorkshop: 'Sign in again before deleting the workshop.',
  22. signInAgainRestore: 'Sign in again before restoring the workshop.',
  23. signInAgainPassword: 'Sign in again before setting a password.',
  24. } as const
  25. export type ReauthError = keyof typeof REAUTH_ERRORS
  26. /** The `settings` message key for a refusal above, or null for any other error. */
  27. export function reauthErrorMessageKey(message: string | undefined): string | null {
  28. const match = (Object.keys(REAUTH_ERRORS) as ReauthError[]).find(
  29. (key) => REAUTH_ERRORS[key] === message
  30. )
  31. if (!match) return null
  32. return `account.reauth${match.charAt(0).toUpperCase()}${match.slice(1)}`
  33. }