oidc-react
oidc-react copied to clipboard
Renew token
Hi. I'm adding middleware to the API requests, I want to detect if the token expired(Unauthorized/401). When the response is forbidden I want to silent sign in to continu to use the site. I tried to use "automaticSilentRenew" but it won't work if the PC is in sleep mode. When you connect in the morning the token will not refresh unless you refresh the page. I also tried to "let tmp = new UserManager(OpenIdConfig).signinCallback();" But it didn't work. Is there a way to acces the sign in function from a ts file that isn't a component without using the hooks?
Hi!
Thanks for reaching out. Maybe you can hook onto something else, like a window active event?
I haven't heard anything back on this, so I'll close this for now. Feel free to open a new issue or comment if you still have issues :)
Just as Tipp in general: If you want to access hook methods in non react components, you can do this by using just a variable and a setter for this variable that is called in a useEffect hook.
For example: If you habe a Client.ts file were you want to use the signIn function, you can do this by:
let signInFunc: () => void;
export const setupSignInFunc(newSinInFunc: () => void) => {
signInFunc = newSinInFunc;
}
...
you can now access the function anywhere in the file
...
Now you need to call the setupSignInFunc in a component that uses the hook. You can call the method in a useEffect.
import { signInFunc } from 'Client.ts';
...
export const MyComponent = () => {
const auth = useAuth();
useEffect(() => {
setupSignInFunc(auth.signIn);
}, [auth];
...
}
Note that i just wrote this without checking types etc. But the concept should work for all cases where you want to use hook functions outside components.