LCOV - code coverage report
Current view: top level - packages/account-core/src - firebaseAuthError.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 39.1 % 23 9
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : /**
       2              :  * Classification of Firebase Auth failures, shared so both hosts branch on
       3              :  * the same buckets. Typed structurally rather than via `instanceof
       4              :  * FirebaseError` so this package needs no firebase dependency — a host passes
       5              :  * whatever it caught and unknown shapes land in 'unknown'.
       6              :  */
       7              : 
       8              : export interface FirebaseAuthErrorLike {
       9              :   code: string
      10              :   message: string
      11              : }
      12              : 
      13              : /**
      14              :  * The user or their browser dismissed/blocked the popup — an outcome to warn
      15              :  * about and retry on a fresh gesture, never an app fault.
      16              :  */
      17         2356 : const POPUP_DISMISSED_CODES: readonly string[] = [
      18              :   'auth/popup-closed-by-user',
      19              :   'auth/cancelled-popup-request',
      20              :   'auth/popup-blocked'
      21              : ]
      22              : 
      23              : /**
      24              :  * The origin is not on the Firebase authorized-domains list (or a continue
      25              :  * URI is unauthorized): auth cannot work here at all until configuration
      26              :  * changes, so the host should say so rather than offer a retry.
      27              :  */
      28         2356 : const UNAUTHORIZED_DOMAIN_CODES: readonly string[] = [
      29              :   'auth/unauthorized-domain',
      30              :   'auth/invalid-dynamic-link-domain',
      31              :   'auth/unauthorized-continue-uri'
      32              : ]
      33              : 
      34              : export type AuthErrorClassification =
      35              :   | { kind: 'unauthorized-domain'; code: string }
      36              :   | { kind: 'signup-blocked'; code: string }
      37              :   | { kind: 'popup-dismissed'; code: string }
      38              :   | { kind: 'auth'; code: string }
      39              :   | { kind: 'unknown' }
      40              : 
      41            1 : export function isFirebaseAuthErrorLike(
      42              :   error: unknown
      43              : ): error is FirebaseAuthErrorLike {
      44            1 :   return (
      45              :     typeof error === 'object' &&
      46              :     error !== null &&
      47              :     'code' in error &&
      48              :     typeof error.code === 'string' &&
      49              :     error.code.startsWith('auth/') &&
      50              :     'message' in error &&
      51              :     typeof error.message === 'string'
      52              :   )
      53              : }
      54              : 
      55            1 : export function classifyAuthError(error: unknown): AuthErrorClassification {
      56            1 :   if (!isFirebaseAuthErrorLike(error)) return { kind: 'unknown' }
      57            0 :   if (UNAUTHORIZED_DOMAIN_CODES.includes(error.code)) {
      58            0 :     return { kind: 'unauthorized-domain', code: error.code }
      59              :   }
      60              :   // Match on `error.message`, not `error.code`: Firebase `beforeUserCreated`
      61              :   // rejections collapse the thrown code into a generic `auth/internal-error`,
      62              :   // so the message is the only reliable channel. `signup_blocked` is a
      63              :   // cross-repo contract token; matched case-insensitively.
      64            0 :   if (error.message.toLowerCase().includes('signup_blocked')) {
      65            0 :     return { kind: 'signup-blocked', code: error.code }
      66              :   }
      67            0 :   if (POPUP_DISMISSED_CODES.includes(error.code)) {
      68            0 :     return { kind: 'popup-dismissed', code: error.code }
      69              :   }
      70            0 :   return { kind: 'auth', code: error.code }
      71              : }
      72              : 
      73              : export type AuthToastSeverity = 'error' | 'warning'
      74              : 
      75              : /**
      76              :  * The cloud app's toast-severity policy for classified auth failures: a
      77              :  * dismissed popup is the user changing their mind, not an application
      78              :  * error; everything else alarms.
      79              :  */
      80            1 : export function severityForAuthError(
      81              :   classification: AuthErrorClassification
      82              : ): AuthToastSeverity {
      83            1 :   return classification.kind === 'popup-dismissed' ? 'warning' : 'error'
      84              : }
      85              : 
      86              : /**
      87              :  * A host's auth error table: Firebase codes mapped to that host's copy, plus
      88              :  * the two fallbacks every table must carry. Hosts build this from their own
      89              :  * i18n; the package ships no strings of its own.
      90              :  */
      91              : export type AuthErrorCopy = Readonly<Record<string, string>> & {
      92              :   readonly generic: string
      93              :   readonly signupBlocked: string
      94              : }
      95              : 
      96              : /** Resolved to the invalid-credential line whatever table is in play. */
      97         2356 : const ENUMERATION_NEUTRAL_CODES: ReadonlySet<string> = new Set([
      98              :   'auth/user-not-found',
      99              :   'auth/wrong-password'
     100              : ])
     101              : 
     102              : /**
     103              :  * The detail copy for a classified failure, resolved against the host copy:
     104              :  * a code the table knows gets its own line, anything else the generic line, a
     105              :  * blocked sign-up its named copy. The user-not-found / wrong-password pair
     106              :  * collapses to the invalid-credential line so a sign-in attempt can never
     107              :  * reveal whether an email has an account. Unauthorized domains need the
     108              :  * host's domain and support address, which the host interpolates itself.
     109              :  */
     110            0 : export function authErrorMessage(
     111              :   classification: AuthErrorClassification,
     112              :   copy: AuthErrorCopy
     113              : ): string {
     114            0 :   switch (classification.kind) {
     115              :     case 'signup-blocked':
     116            0 :       return copy.signupBlocked
     117              :     case 'popup-dismissed':
     118              :     case 'auth':
     119            0 :       if (ENUMERATION_NEUTRAL_CODES.has(classification.code)) {
     120            0 :         return copy['auth/invalid-credential'] ?? copy.generic
     121              :       }
     122            0 :       return copy[classification.code] ?? copy.generic
     123              :     case 'unauthorized-domain':
     124              :     case 'unknown':
     125            0 :       return copy.generic
     126              :   }
     127              : }
        

Generated by: LCOV version 2.0-1