soofstad / react-oauth2-pkce

Provider agnostic OAuth2 Authorization Code flow with PKCE for React
MIT License
125 stars 53 forks source link

Bug: Token is cleared before Logout #169

Closed tomekxoxo closed 5 months ago

tomekxoxo commented 5 months ago

I want to logout from my application in Axios interceptor when i receive 401 code. It works with setTimeout set to 5seconds, but it dose not without it. On the screenshot token is empty in if statement. Also why do you clear token before checking if it exist? I use: "axios": "^1.3.2", "react-oauth2-code-pkce": "^1.20.0",

Screenshots

Screenshot 2024-06-04 at 12 05 53 Screenshot 2024-06-04 at 12 11 51

The expected behavior

Token is not cleared before checking if it exist to be able to logout

soofstad commented 5 months ago

Hi, thanks for reporting @tomekxoxo I understand why this looks strange, and should perhaps be written in a different way. But I think there is something else going on here.

Reacts 'useState' is asynchronous, so if I access the variable right after its set, in an synchronous code block, I will in fact be referring to the old value.

Could be that the reliance on this causes issues if you are doing something funky by intercepting requests.

If token was empty, you should never be redirected, and therefore never get a 401.

If you do get redirected, but no token in the request, that would be very strange.

Can you provide full authConfig, console log, and network requests for further debugging?

tomekxoxo commented 5 months ago

I deliberately cut out a token from a network request to receive 401. Could that be an issue?

tomekxoxo commented 5 months ago

I think I found an issue in my interceptor, sorry for false alarm

tomekxoxo commented 5 months ago

Yeah i didn't eject interceptors when data changes :)