fastify-file-upload
fastify-file-upload copied to clipboard
Upload file before jwt verify

Although jwt expired, still upload files to server? How do I solve this problem?


You can use fastify's preHandler hook

Example:
export function jwtAuthMiddleware(
request: FastifyRequest,
reply: FastifyReply,
done: HookHandlerDoneFunction
) {
// get auth token from header
const token = request.headers['x-auth-token']
if (!token) {
return reply.status(401).send({ msg: 'No token, authorization denied' })
}
if (typeof token !== 'string') {
return reply.status(401).send({ msg: 'Invalid token format' })
}
try {
// verify token
const decoded = verifyToken(token)
request.user = { id: decoded.id, username: decoded.username, verified: decoded.verified }
done()
} catch (e) {
reply.status(400).send({ msg: 'Token is not valid' })
}
done()
}