fastify-file-upload icon indicating copy to clipboard operation
fastify-file-upload copied to clipboard

Upload file before jwt verify

Open dungnt11 opened this issue 3 years ago • 1 comments

image

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

image

image

dungnt11 avatar Jul 05 '22 03:07 dungnt11

You can use fastify's preHandler hook image

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()
}

olingern avatar Oct 08 '22 16:10 olingern