AxaFrance / oidc-client

Light, Secure, Pure Javascript OIDC (Open ID Connect) Client. We provide also a REACT wrapper (compatible NextJS, etc.).
MIT License
571 stars 152 forks source link

How to do a refetch process with react-oidc #1324

Open MichalJana opened 3 months ago

MichalJana commented 3 months ago

Hi, I have a question,

When using @axa-fr/react-oidc with RTK Query or Axios, it's not possible to call refreshToken and use refetch process when a 401 error is returned. The issue is that refreshToken cannot be called outside of a tsx component.

Here is an example of how refetch is done in RTK Query, and it's similar in Axios or any other similar libraries as well: Automatic re-authorization by extending fetchBaseQuery

How do you address this situation?

meesvandongen commented 3 months ago

You could use

import { OidcClient } from '@axa-fr/oidc-client';

await getOidc().renewTokensAsync();
guillaume-chervet commented 3 months ago

The librairy refresh tokens automaticaly in background. The clean way to use it is here: https://github.com/AxaFrance/oidc-client/blob/main/packages/oidc-client/src/fetch.ts

like @meesvandongen said upper, you can use directly @axa-fr/oidc-client.

MichalJana commented 3 months ago

Hi, using @axa-fr/oidc-client would mean I'd have to unnecessarily create a wrapper that is already provided by @axa-fr/react-oidc. Is it possible to use both packages in the project simultaneously?

guillaume-chervet commented 3 months ago

Yes it is possible @MichalJana