All files / features/auth decideLinkVsRecover.ts

100% Statements 2/2
100% Branches 4/4
100% Functions 1/1
100% Lines 2/2

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21                      1x             7x    
/**
 * Anonymous-first identity: connecting a provider first tries to LINK it onto the
 * current anonymous account. If that provider/email already belongs to another
 * Firebase user (reinstall, new device, partner re-connecting), the link fails and
 * we must instead SIGN IN to recover that existing account — discarding the
 * throwaway anonymous uid. This pure helper maps the link error to that decision.
 */
export type LinkFailureAction = "recover" | "rethrow";
 
// Firebase Auth error codes that mean "this credential already identifies an account",
// i.e. the user should be recovered via sign-in rather than linked.
const RECOVERABLE_CODES = new Set([
  "auth/credential-already-in-use",
  "auth/email-already-in-use",
  "auth/account-exists-with-different-credential",
]);
 
export function decideLinkVsRecover(errorCode: string | undefined): LinkFailureAction {
  return errorCode && RECOVERABLE_CODES.has(errorCode) ? "recover" : "rethrow";
}