Ícaro Cedraz
Ícaro Cedraz

Reputation: 1

How to specify which JWT SECRET to use to create a JWT and verify a JWT with Fastify?

I'm building an API using Fastify and fastify/jwt and I'm having trouble specifying which secret I'm going to use to create and verify a JWT.

I have two different JWT's: "refresh-token" and "token".

For "refresh-token", I'm going to use a JWT_REFRESH_SECRET, and for "token" I'm going to use "JWT_SECRET".

Here is my "app.ts", where I configure my fastifyJwt:

app.register(fastifyJwt, {
    secret: env.JWT_SECRET,
    cookie:{
        cookieName: 'refreshToken',
        signed: false
    },
    sign: {
        expiresIn: '10m'
    }
})

using:

await request.jwtVerify({ 
    onlyCookie: true
})

How can I specify the secret I'm going to use to decode this JWT?

const token = await reply.jwtSign({
    sub: request.user.sub,
})

And to create the JWT, how can I specify the secret to create it? I tried using:

await request.jwtVerify({ 
    onlyCookie: true,
    secret: env.JWT_REFRESH_SECRET
})

but I always get an error:

No overload matches this call. Overload 1 of 5, '(options?: FastifyJwtVerifyOptions | undefined): Promise', gave the following error. Object literal may only specify known properties, and 'onlyCookie' does not exist in type 'FastifyJwtVerifyOptions'. Overload 2 of 5, '(callback: VerifierCallback): void', gave the following error. Object literal may only specify known properties, and 'onlyCookie' does not exist in type 'VerifierCallback'. Overload 3 of 5, '(options?: Partial | undefined): Promise', gave the following error. Object literal may only specify known properties, and 'secret' does not exist in type 'Partial'.ts(2769) (method) FastifyRequest<RouteGenericInterface, RawServerDefault, IncomingMessage, FastifySchema, FastifyTypeProviderDefault, unknown, FastifyBaseLogger, ResolveFastifyRequestType<...>>.jwtVerify(options?: FastifyJwtVerifyOptions | undefined): Promise (+4 overloads)

Upvotes: 0

Views: 286

Answers (0)

Related Questions