forked from vhaudiquet/rhema-together
Accounts get a profile page with an uploaded picture, shown in the sidebar, the room bar, and every human message. Pictures live in the SQLite database and are served through an authenticated route. Hardening from the security audit: preview hosts expire after 24 hours and carry no-referrer; removing a member or archiving a room ends their event feed, presence socket, and terminal within seconds; previews and the terminal pin the SSH host key; invites are claimed atomically; missing accounts spend the same argon2 time as wrong passwords; registration is closed by default; WebSockets check the origin; and the master key is created with mode 0600. The Dockerfile builds one image with the static client and the server. The Helm chart runs a single replica with a SQLite volume, a wildcard preview ingress, and the proxy settings the event feed and uploads need. It refuses more than one replica because state is in process.
61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import { ApiRequestError, normalizeApiError } from '~/utils/errors'
|
|
|
|
type ApiMethod = 'GET' | 'POST' | 'PATCH' | 'DELETE'
|
|
|
|
interface RequestOptions {
|
|
method?: ApiMethod
|
|
body?: unknown
|
|
query?: Record<string, string | number | boolean | undefined>
|
|
contentType?: string
|
|
}
|
|
|
|
const PUBLIC_PREFIXES = ['/setup', '/login', '/register', '/join']
|
|
|
|
export function useApi() {
|
|
const router = useRouter()
|
|
const route = useRoute()
|
|
const csrf = useCookie<string | null>('together_csrf', { sameSite: 'lax' })
|
|
|
|
function mayRedirect() {
|
|
return !PUBLIC_PREFIXES.some((prefix) => route.path === prefix || route.path.startsWith(`${prefix}/`))
|
|
}
|
|
|
|
async function request<T>(path: string, options: RequestOptions = {}): Promise<T> {
|
|
const method = options.method ?? 'GET'
|
|
const headers: Record<string, string> = { Accept: 'application/json' }
|
|
if (options.contentType) headers['Content-Type'] = options.contentType
|
|
if (method !== 'GET' && csrf.value) headers['X-CSRF-Token'] = csrf.value
|
|
try {
|
|
return await $fetch<T>(path, {
|
|
baseURL: '/api',
|
|
method,
|
|
body: options.body as Record<string, unknown> | undefined,
|
|
query: options.query,
|
|
headers,
|
|
credentials: 'include',
|
|
retry: 0,
|
|
})
|
|
} catch (error) {
|
|
const failure = normalizeApiError(error)
|
|
if (failure.status === 401 && mayRedirect()) {
|
|
void router.replace('/login')
|
|
}
|
|
throw failure
|
|
}
|
|
}
|
|
|
|
return {
|
|
get: <T>(path: string, query?: RequestOptions['query']) => request<T>(path, { query }),
|
|
post: <T>(path: string, body?: unknown) => request<T>(path, { method: 'POST', body }),
|
|
upload: <T>(path: string, body: Blob) =>
|
|
request<T>(path, {
|
|
method: 'POST',
|
|
body,
|
|
contentType: body.type || 'application/octet-stream',
|
|
}),
|
|
patch: <T>(path: string, body?: unknown) => request<T>(path, { method: 'PATCH', body }),
|
|
del: <T>(path: string) => request<T>(path, { method: 'DELETE' }),
|
|
ApiRequestError,
|
|
}
|
|
}
|