nuxt-community / auth-module

Zero-boilerplate authentication support for Nuxt 2
https://auth.nuxtjs.org
MIT License
1.93k stars 924 forks source link

How to logout a user if the server rejected the token? #1688

Open Venegrad opened 2 years ago

Venegrad commented 2 years ago

How to logout a user if the server rejected the token? The application automatically sends a "user" request, if the token is rejected, how do I log out the user at this moment?

ayoubbajjou commented 2 years ago

@Venegrad What about autoLogout ? If the token has expired, it will prevent the token from being refreshed on the reload of the page and will force the user to logout

ViktorCollin commented 2 years ago

I have a similar issue where a endpoint may return a 401 Unauthorized even if the token is not expired due to tokens being revoked. I have not found any way on handling that except implement my own interceptor for the axios module. I my case when i detect a 401 response i frist try to refresh tokens and if taht worked the replay the request once if it is an idempotent request and if that still gives a 401 then I log the user out.

trandaison commented 1 year ago

@ayoubbajjou Nah, different case here. It's not about token expires, it's about invalid token, might be server deleted tokens when user reset password on an other browser. Or some how server response an 401 error (the token still not expired).

@Venegrad In this case, you can call $auth.reset() to clear auth data, then redirect to login route. But if you are using SPA mode, it's not easy to redirect inside plugin 😢