How to handle user logout when using useUser hook with useSWR? #870
Replies: 3 comments 7 replies
-
Hey ! I'm facing the same issue while handling authentication with SWR, did you find any solutions ? Have a good day Edit : after reflection, i'm thinking that maybe for authentication part it's not the best idea to use SWR.. |
Beta Was this translation helpful? Give feedback.
-
Hi, there are multiple solutions to your problem. For instance you could use a custom fetcher for handling your server response :
that way your |
Beta Was this translation helpful? Give feedback.
-
When user logs out, you can use the followng method to clear the cache from the SWR Map, instead of mutating every endpoint. Since it's not just login, but also there will be other endpoints which might be user specific and we need to clear those cache as well.
|
Beta Was this translation helpful? Give feedback.
-
I use cookies and sessions for user authentication. When the cookie expires and user makes a request for some data it returns error because user is no longer logged in.
What is the practice for handling this scenario with the pattern of useUser hook combined with useSWR?
Where should I handle this error and where should I use
mutate()
to update the user info and where should I do a navigate to the home page when it happens?I know this is not fully SWR related question but I really have no idea how to deal with it.
I would really appreciate some tips for using useUser hook and useSWR.
Beta Was this translation helpful? Give feedback.
All reactions