Files
ChatLab/electron/main/api/auth.ts
T
2026-03-28 00:10:29 +08:00

26 lines
741 B
TypeScript

/**
* ChatLab API — Bearer Token authentication hook
*/
import type { FastifyRequest, FastifyReply } from 'fastify'
import { loadConfig } from './config'
import { unauthorized, errorResponse } from './errors'
export async function authHook(request: FastifyRequest, reply: FastifyReply): Promise<void> {
const authHeader = request.headers.authorization
if (!authHeader || !authHeader.startsWith('Bearer ')) {
const err = unauthorized()
reply.code(err.statusCode).send(errorResponse(err))
return
}
const token = authHeader.slice(7)
const config = loadConfig()
if (!config.token || token !== config.token) {
const err = unauthorized()
reply.code(err.statusCode).send(errorResponse(err))
return
}
}