All files / packages / shared/src session.ts

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

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 86 87 88 89 90 91 92 93 94 95 96 97              4x                     4x                   4x           4x                       4x             4x                   4x               4x             4x   4x                                
import { z } from 'zod'
import type { Endpoint } from './endpoint'
 
// Sign-in and sessions (design 4.1, 4.2, 5.1). The Firebase ID token travels in the
// Authorization header; only bootstrap also sends Google's ID token, which carries `hd`.
 
// Codes the web switches on, beside ERROR_CODE (design G19).
export const AUTH_ERROR_CODE = {
  invalidToken: 'invalid_token',
  domainNotAllowed: 'domain_not_allowed',
  emailNotVerified: 'email_not_verified',
  notRegistered: 'not_registered',
  accountDeactivated: 'account_deactivated',
  sessionEnded: 'session_ended',
  originRefused: 'origin_refused',
  emailInUse: 'email_in_use',
} as const
 
export const SESSION = {
  pageMax: 50,
  // Value lists the users columns (schema.ts, their CHECKs) read; see CHANNEL.kinds.
  userRoles: ['owner', 'admin', 'member'],
  userStatuses: ['active', 'deactivated'],
} as const
 
export type UserRole = (typeof SESSION.userRoles)[number]
export type UserStatus = (typeof SESSION.userStatuses)[number]
 
export const BootstrapRequestSchema = z.object({
  googleIdToken: z.string().min(1).max(8192),
})
 
export type BootstrapRequest = z.infer<typeof BootstrapRequestSchema>
 
export const MeSchema = z.object({
  id: z.string(),
  workspaceId: z.string(),
  email: z.string(),
  displayName: z.string(),
  avatarUrl: z.string().nullable(),
  // Open, not z.enum: a role added later must not break older web clients.
  role: z.string(),
})
 
export type Me = z.infer<typeof MeSchema>
 
export const BootstrapResponseSchema = z.object({
  user: MeSchema,
  sessionId: z.string(),
})
 
export type BootstrapResponse = z.infer<typeof BootstrapResponseSchema>
 
export const SessionSchema = z.object({
  id: z.string(),
  device: z.string().nullable(),
  createdAt: z.number().int(),
  lastSeenAt: z.number().int(),
  current: z.boolean(),
})
 
export type Session = z.infer<typeof SessionSchema>
 
export const SessionListQuerySchema = z.object({
  limit: z.coerce.number().int().min(1).max(SESSION.pageMax).default(20),
  // The last id of the previous page: ids are UUIDv7, so they sort by creation time.
  cursor: z.uuid().optional(),
})
 
export type SessionListQuery = z.infer<typeof SessionListQuerySchema>
 
export const SessionPageSchema = z.object({
  sessions: z.array(SessionSchema),
  nextCursor: z.string().nullable(),
})
 
export type SessionPage = z.infer<typeof SessionPageSchema>
 
export const SessionIdParamSchema = z.object({ id: z.uuid() })
 
export const SESSION_API = {
  bootstrap: {
    method: 'POST',
    path: '/api/session/bootstrap',
    body: BootstrapRequestSchema,
    response: BootstrapResponseSchema,
  },
  signOutEverywhere: { method: 'POST', path: '/api/session/sign-out-everywhere' },
  list: {
    method: 'GET',
    path: '/api/sessions',
    query: SessionListQuerySchema,
    response: SessionPageSchema,
  },
  end: { method: 'DELETE', path: '/api/sessions/:id', params: SessionIdParamSchema },
} as const satisfies Record<string, Endpoint>