oidc-react icon indicating copy to clipboard operation
oidc-react copied to clipboard

Renew token

Open dor1202 opened this issue 2 years ago • 1 comments

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?

dor1202 avatar May 02 '22 08:05 dor1202

Hi!

Thanks for reaching out. Maybe you can hook onto something else, like a window active event?

simenandre avatar Jul 15 '22 14:07 simenandre

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 :)

simenandre avatar Mar 01 '23 18:03 simenandre

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.

D4uS1 avatar Aug 14 '23 09:08 D4uS1