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

87.5% Statements 21/24
100% Branches 0/0
88.88% Functions 8/9
87.5% Lines 21/24

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                                        3x 3x 1x       5x 5x 3x                           3x 2x       39x 31x                   3x 3x 1x       1x 1x         4x 4x 4x       1x 1x 1x    
import { httpClient } from '@/lib/http-client'
 
import {
  assistedRequestSchema,
  demoSessionSchema,
  loginSchema,
  okResponseSchema,
  passwordResetVerifySchema,
  passwordlessVerifySchema,
  registerSchema,
  userSchema,
  type AssistedRequestInput,
  type LoginInput,
  type PasswordResetVerifyInput,
  type PasswordlessVerifyInput,
  type RegisterInput,
  type User,
} from './auth.schemas'
 
export async function registerUser(input: RegisterInput): Promise<User> {
  const body = registerSchema.parse(input)
  const response = await httpClient.post<unknown>('/auth/register', body)
  return userSchema.parse(response)
}
 
export async function loginUser(input: LoginInput): Promise<void> {
  const body = loginSchema.parse(input)
  const response = await httpClient.post<unknown>('/auth/login', body)
  okResponseSchema.parse(response)
}
 
/**
 * Starts a session in a new, disposable account the API fills with a sample
 * family and deletes 24 hours later. One account per call, so visitors never
 * see each other's changes.
 */
export async function startDemoSession(): Promise<void> {
  const response = await httpClient.post<unknown>('/auth/demo')
  demoSessionSchema.parse(response)
}
 
export async function logoutUser(): Promise<void> {
  const response = await httpClient.post<unknown>('/auth/logout')
  okResponseSchema.parse(response)
}
 
export async function getCurrentUser(): Promise<User> {
  const response = await httpClient.get<unknown>('/auth/me')
  return userSchema.parse(response)
}
 
/**
 * Assisted sign-in. The request endpoints answer the same way whether or not
 * the address has an account — the UI must never become a way to find out
 * which e-mails are registered — so there is nothing in the response to read
 * beyond "accepted".
 */
export async function requestPasswordlessCode(input: AssistedRequestInput): Promise<void> {
  const body = assistedRequestSchema.parse(input)
  const response = await httpClient.post<unknown>('/auth/passwordless/request', body)
  okResponseSchema.parse(response)
}
 
export async function verifyPasswordlessCode(input: PasswordlessVerifyInput): Promise<void> {
  const body = passwordlessVerifySchema.parse(input)
  const response = await httpClient.post<unknown>('/auth/passwordless/verify', body)
  okResponseSchema.parse(response)
}
 
export async function requestPasswordResetCode(input: AssistedRequestInput): Promise<void> {
  const body = assistedRequestSchema.parse(input)
  const response = await httpClient.post<unknown>('/auth/password-reset/request', body)
  okResponseSchema.parse(response)
}
 
export async function verifyPasswordReset(input: PasswordResetVerifyInput): Promise<void> {
  const body = passwordResetVerifySchema.parse(input)
  const response = await httpClient.post<unknown>('/auth/password-reset/verify', body)
  okResponseSchema.parse(response)
}