I've been trying with little success to implement the token refresh using the oidc-client library. Any help or insight is greatly appreciated.
Stack:
- React
- GraphQL (Apollo Client)
Structure:
I've tried a few approaches, starting with automaticSilentRenew and creating a static-renew.html callback file in my public/ folder - but this both approaches gave me a No state in response error so I set out on a workaround.
Workaround:
I'm wrapping the router in a component which does the following:
on mount it calls
oidc-client.getUser()and then adds the following listeners:oidcMgr.events.addAccessTokenExpiring(handleTokenExpiring)window.addEventListener('message', this.handleSilentRenew)
When the
addAccessTokenExpiringevent first fires it calls a function that:oidcMgr.createSigninRequest({ redirect_uri: process.env.REACT_APP_SILENT_REDIRECT_URI })and sets the response to local state usingapollo-link-stateRenders the iFrame when there is a tokenURI in apollo-link-state (from the function above).
When the component unmounts it removes both listeners.
- When the callback URL is hit, it mounts a component that posts a message with the source
silent-renewso the listener mounted in step 1 can set the token URI to''inapollo-link-statethus terminating the iframe and callsgetUseragain.
This works for refreshing the token but it seems that the iFrame is loading the entire application inside of it, and then terminating, causing the application to consume more resources than necessary.
Is there another, more straightforward way to use this library to refresh the token within a React application? Any help or perspective will be greatly appreciated. Thank you!