nuxt-graphql-client icon indicating copy to clipboard operation
nuxt-graphql-client copied to clipboard

Method fired at accessToken expired like `useGqlTokenExpired` or is using useGqlToken in middleware a good solution?

Open toknT opened this issue 1 year ago • 1 comments

Your use case

I am using accessToken and refreshToken.This package only has useGqlToken so I create a middleware like this

import { AuthService } from "~/services/AuthService";

export default defineNuxtRouteMiddleware(async (to, from) => {
  if (import.meta.server) return;
  try {
    const accessToken = await AuthService.accessToken();
    useGqlToken(accessToken);
  } catch (error) {
    console.error("token error", error);
    return navigateTo("/login");
  }
});

and check if accessToken is expired or refresh a new accessToken with refreshToken in AuthService.accessToken

I am not use if it is a good way.

The solution you'd like

add some method can fired when token (access token) is expired.

Possible alternatives

No response

Additional information

No response

toknT avatar May 09 '24 08:05 toknT

A common pattern and more reliable solution to this would be for useGqlToken to accept a function/async that returns a token (maybe config too if that is sensible). It would be called each time a request is made, where it can check for expiration and attempt to refresh it, then return that instead.

ief015 avatar Jun 13 '25 02:06 ief015