Nextjs JWT authentication with no libraries (Only fetch)
Answered
OxyGen posted this in #help-forum
OxyGenOP
Hey guys, I'm trying to build authentication with no libraries. So I'm on the way of refetching user on each page after reloading or logging in and refreshing access token on expire.
I'm sending the request on each page in navbar to get user from backend and set user state in zustand store. In case of refreshing access token. I'm refreshing it when I receive 403 error.
I've question can somebody explain the logic. Because I think my approach is not good
I'm sending the request on each page in navbar to get user from backend and set user state in zustand store. In case of refreshing access token. I'm refreshing it when I receive 403 error.
I've question can somebody explain the logic. Because I think my approach is not good
19 Replies
You said with no libraries but then you mentioned zustand
what is the issue?
OxyGenOP
I mean no libraries such as Next auth or axios
what session storing strategy did you use? database session id or jwt?
OxyGenOP
Sorry for confusion. I use jwt
what is your approach? why do you think its not good?
OxyGenOP
I think that sending request to get user on each page is not good. Maybe there is better solution. But I didn't find out so I ask this question
@OxyGen I think that sending request to get user on each page is not good. Maybe there is better solution. But I didn't find out so I ask this question
if your user state is already stored in jwt, why get user on each page?
just get user from jwt
OxyGenOP
Because user's username, picture and other stuff is getting from backend
but the sessionid is store din jwt right?
@aardani but the sessionid is store din jwt right?
OxyGenOP
userid is stored in jwt
@OxyGen userid is stored in jwt
does zustand provide client-side caching?
because RQ and useSWR does
so that you dont ened to fetch every navigation
and can declare duration on how old data is considered fresh
Answer
OxyGenOP
is it the same as revalidation in fetch?
revalidation is server-side caching