All files / src/server/auth auth.ts

100% Statements 1/1
100% Branches 0/0
100% Functions 0/0
100% Lines 1/1

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 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60            9x                                                                                                          
// Signing in, independent of the auth library (CLAUDE.md, "Cloudflare stays
// inside src/server/platform/"). Server code gets an `Auth` from the request
// context; Better Auth behind it lives in src/server/platform/auth.server.ts.
import type { Locale } from '#/paraglide/runtime.js'
 
/** Sign-in code rules (spec: "Rejecting wrong and expired codes"). */
export const SIGN_IN_CODE = {
  length: 6,
  validSeconds: 10 * 60,
  attempts: 5,
} as const
 
export interface AuthUser {
  id: string
  email: string
  /** The display name; empty until the user has given one. */
  name: string
  /** The language stored on the account. */
  locale: Locale
}
 
export interface AuthSession {
  user: AuthUser
  expiresAt: Date
}
 
/**
 * Why a code did not sign the user in:
 * - `code_wrong`: not the code that was sent (or no code was sent)
 * - `code_expired`: older than its validity
 * - `code_attempts_used`: too many wrong attempts; a new code is needed
 */
export type SignInFailure = 'code_wrong' | 'code_expired' | 'code_attempts_used'
 
export type SignInResult =
  { ok: true; user: AuthUser } | { ok: false; failure: SignInFailure }
 
export interface Auth {
  /**
   * Creates a 6-digit sign-in code for the address and returns it, so the
   * caller can email it. A new code replaces any earlier one.
   */
  createSignInCode(email: string): Promise<string>
  /**
   * Signs in with a code and sets the session cookie. An address without an
   * account gets one, with `newUserLocale` as its language.
   */
  signInWithCode(
    email: string,
    code: string,
    newUserLocale: Locale,
  ): Promise<SignInResult>
  /** The current request's session, or null when not signed in. */
  getSession(): Promise<AuthSession | null>
  /** Ends the current session on the server and clears its cookie. */
  signOut(): Promise<void>
  /** The stored language of the account with this address, if any. */
  findLocale(email: string): Promise<Locale | null>
}