fastify / fastify-oauth2

Enable to perform login using oauth2 protocol
MIT License
242 stars 68 forks source link

TypeScript error for FastifyOauth2 #264

Open johnkmzhou opened 6 days ago

johnkmzhou commented 6 days ago

Prerequisites

Fastify version

4.28.1

Plugin version

No response

Node.js version

18

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

10

Description

Getting this TypeScript error:

No overload matches this call. Overload 2 of 3, '(plugin: FastifyPluginAsync<FastifyOAuth2Options, RawServerDefault, FastifyTypeProviderDefault, FastifyBaseLogger>, opts?: FastifyRegisterOptions<...> | undefined): FastifyInstance<...> & PromiseLike<...>', gave the following error. Argument of type 'FastifyOauth2' is not assignable to parameter of type 'FastifyPluginAsync<FastifyOAuth2Options, RawServerDefault, FastifyTypeProviderDefault, FastifyBaseLogger>'. Target signature provides too few arguments. Expected 3 or more, but got 2.

This looks to be similar to #120.

Steps to reproduce

Run the following code with TypeScript (environment variables will need to be provided):

// error will occur in register function
fastify.register(oauthPlugin, {
  name: 'custom',
  scope: ['openid'],
  credentials: {
    client: {
      id: process.env.CLIENT_ID,
      secret: process.env.CLIENT_SECRET,
    },
  },
  startRedirectPath: '/login',
  callbackUri: process.env.CALLBACK_URI,
  discovery: {
    issuer: process.env.DISCOVERY_ENDPOINT,
  },
});

fastify.get('/callback', async (req, reply) => {
  try {
    const { token } = await fastify.custom.getAccessTokenFromAuthorizationCodeFlow(req);

    reply.send(token);
  } catch (e) {
    fastify.log.error(e);
  }
});

No response

Expected Behavior

No TypeScript error.

mcollina commented 6 days ago

Thanks for reporting!

Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.

johnkmzhou commented 3 days ago

Thanks for reporting!

Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.

I updated the initial post with a code snippet that should help.