/
64mb
/
copy-service
Обзор
Документация
Войти
/
64mb
/
copy-service
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
master
plugin/auth.js
39 строк
799 B
Georgy
init
01 окт 2022, 02:10
01 окт 2022, 02:10
f261e97
Код
Авторство
О чём код?
const fp = require('fastify-plugin') const jwt = require('jsonwebtoken') module.exports = fp((fastify, { header = 'authorization', secret }, next) => { if (secret == null) { throw new Error('auth plugin secret undefined') } async function check(token) { if (token == null) return null let decode = null try { decode = jwt.verify(token, secret) } catch (err) { return null } return decode } async function handler(request, reply) { const result = await check(request.headers ? request.headers[header] : null) request.auth = null if (result == null) { reply.code(403) return reply.send({ error: 'auth required' }) } request.auth = result return result } fastify.decorate('$auth', handler) next() })