Skip to content

Type the authenticate decorator instead of using any #7

Description

@Abioladory123

Context

src/server.ts:

server.decorate("authenticate", async function (request: any, reply: any) {
  try {
    await request.jwtVerify();
  } catch (err) {
    reply.send(err);
  }
});

Two things worth fixing here. The any parameters mean route handlers get no type safety on request.user, and because the decorator isn't declared on FastifyInstance, every call site referencing fastify.authenticate is either untyped or cast.

Separately, the catch calls reply.send(err) without an explicit status. It happens to produce a 401 because @fastify/jwt errors carry a statusCode, but that's implicit and easy to break.

What to do

  • Use FastifyRequest / FastifyReply types
  • Add a module augmentation declaring authenticate on FastifyInstance, and the JWT payload shape (sub, wallet, slug — see verifyChallenge in src/modules/auth/auth.service.ts) on @fastify/jwt
  • Set the status code explicitly before sending

Acceptance criteria

  • No any in the decorator
  • request.user is typed at call sites
  • npm run typecheck passes

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions