init project
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Простая защита админ-роутов: заголовок Authorization: Bearer <ADMIN_API_TOKEN>
|
||||
*/
|
||||
export function registerAuth(fastify) {
|
||||
fastify.decorate('verifyAdmin', async function verifyAdmin(request, reply) {
|
||||
const token = process.env.ADMIN_API_TOKEN
|
||||
if (!token) {
|
||||
return reply.code(503).send({ error: 'ADMIN_API_TOKEN не задан в .env' })
|
||||
}
|
||||
const auth = request.headers.authorization
|
||||
const match = typeof auth === 'string' ? auth.match(/^Bearer\s+(.+)$/i) : null
|
||||
if (!match?.[1] || match[1] !== token) {
|
||||
return reply.code(401).send({ error: 'Неверный или отсутствующий токен' })
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user