Skip to content

getAccessTokenFromRefreshToken expects refresh_token to be of type Token instead of string #174

@frixaco

Description

@frixaco

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.4.0

Plugin version

6.0.0

Node.js version

16.6.0

Operating system

macOS

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

12.3.1

Description

Hello! I'm new to Fastify. Currently trying to set up Github OAuth2 and I can't renew my access token using refresh token with getNewAccessTokenUsingRefreshToken because the method expects Token instead of string. Here's simple snippet:

const refresh_token = request.body.refresh_token; // string
const newToken = await fastify.githubOAuth2.getNewAccessTokenUsingRefreshToken(refresh_token, {});

gives type error Argument of type 'string' is not assignable to parameter of type 'Token'.

I tried fixing the type error as follows, but I'm getting { "error": "bad_refresh_token", "error_description": "The refresh token passed is incorrect or expired." } error response:

const newToken =await fastify.githubOAuth2.getNewAccessTokenUsingRefreshToken(
  {
    access_token: expiredAccessToken, // string   <-- passing not expired access token also doesn't work
    refresh_token: refreshToken, // string
    token_type: "bearer",
    expires_in: eightHoursFromNow, // number (seconds)
    expires_at: new Date(eightHoursFromNow),
  },
  {},
);

Maybe I'm doing something wrong? I would highly appreciate any help/tips/ideas.

Steps to Reproduce

// index.ts
import fastify, {
  FastifyInstance,
  FastifyReply,
  FastifyRequest,
} from "fastify";
import fp, { PluginMetadata } from "fastify-plugin";

const app = fastify();
app.register(fastifyOauth2, {
  name: "githubOAuth2",
  credentials: {
    client: {
      id: process.env.GITHUB_CLIENT_ID,
      secret: process.env.GITHUB_CLIENT_SECRET,
    },
    auth: fastifyOauth2.GITHUB_CONFIGURATION,
  },
  startRedirectPath: "/login/oauth/github",
  callbackUri: "http://localhost:4000/login/oauth/github/callback",
  scope: [],
});
app.register(
  fp(async function (fastify: FastifyInstance, opts: PluginMetadata) {
    fastify.decorate(
      "verifyUser",
      async function ( request: FastifyRequest<{Body: { refreshToken: string } }>, reply: FastifyReply, next: (error?: object) => void) {
        const refreshToken = request.body.refreshToken;
        const newToken = await fastify.githubOAuth2.getNewAccessTokenUsingRefreshToken(refreshToken, {});
        next();
      }
    )}))

app.register(import("@fastify/auth"));
app.after((err) => {});
app.register(fp(privateRoutes));

app.listen({ port: PORT });

// privateRoutes.ts
import { FastifyInstance, FastifyPluginOptions } from "fastify";

export async function privateRoutes( fastify: FastifyInstance, options: FastifyPluginOptions ) {
  fastify.get<{ Body: { refreshToken: string, .... } }>(
    "/project",
    { preHandler: fastify.auth([fastify.verifyUser]) },
    async function (request, reply) {
      reply.send({ success: true });
    },
  );
}

Expected Behavior

Passing refreshToken which is string to await fastify.githubOAuth2.getNewAccessTokenUsingRefreshToken(refreshToken, {}) should give me new access token.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions