All files / src/features/auth/api auth.schemas.ts

100% Statements 13/13
100% Branches 0/0
100% Functions 2/2
100% Lines 13/13

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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85    14x             14x           14x                         14x           14x                   14x                 14x   5x   14x           14x               14x         1x           14x    
import { z } from 'zod'
 
export const registerSchema = z.object({
  name: z.string().min(1),
  email: z.string().email(),
  password: z.string().min(8),
})
export type RegisterInput = z.infer<typeof registerSchema>
 
export const loginSchema = z.object({
  email: z.string().email(),
  password: z.string().min(1),
})
export type LoginInput = z.infer<typeof loginSchema>
 
export const userSchema = z.object({
  id: z.string().uuid(),
  email: z.string().email(),
  name: z.string(),
  createdAt: z.string(),
  avatarUrl: z.string().nullable().optional(),
  // When a disposable demo account is deleted; null for every real account.
  // Optional as well as nullable so a response from before API #42 still parses.
  demoExpiresAt: z.string().nullable().optional(),
})
export type User = z.infer<typeof userSchema>
 
/** POST /auth/demo: the session cookies come with it; the body says when the account ends. */
export const demoSessionSchema = z.object({
  status: z.literal('ok'),
  message: z.string(),
  expiresAt: z.string(),
})
 
export const okResponseSchema = z.object({
  status: z.literal('ok'),
  message: z.string(),
})
 
/**
 * The assisted sign-in flows: "sign in without a password" and "reset my
 * password". Both are the same machine — e-mail, then a 6-digit code — and
 * reset inserts one extra step before it ends in a session.
 */
export const assistedRequestSchema = z.object({
  email: z.string().email(),
})
export type AssistedRequestInput = z.infer<typeof assistedRequestSchema>
 
// Refined rather than `.length(6).regex(...)` so the message is a single i18n
// key: zod-error.ts maps a `custom` issue's message straight to a key, while
// the built-in codes would collapse "too short" and "not digits" into the same
// generic "invalid format".
const codeField = z
  .string()
  .refine((value) => /^\d{6}$/.test(value), { message: 'auth.assisted.codeFormat' })
 
export const passwordlessVerifySchema = z.object({
  email: z.string().email(),
  code: codeField,
})
export type PasswordlessVerifyInput = z.infer<typeof passwordlessVerifySchema>
 
export const passwordResetVerifySchema = z.object({
  email: z.string().email(),
  code: codeField,
  password: z.string().min(8),
})
export type PasswordResetVerifyInput = z.infer<typeof passwordResetVerifySchema>
 
/** The two password fields as the form collects them, before the code is added. */
export const newPasswordSchema = z
  .object({
    password: z.string().min(8),
    confirmPassword: z.string().min(1),
  })
  .refine((value) => value.password === value.confirmPassword, {
    path: ['confirmPassword'],
    message: 'auth.assisted.passwordMismatch',
  })
export type NewPasswordInput = z.infer<typeof newPasswordSchema>
 
export const codeFormSchema = z.object({ code: codeField })
export type CodeFormInput = z.infer<typeof codeFormSchema>