Next.js Discord

Discord Forum

How to update the user session on server side

Unanswered
Eyebrowed Thrush posted this in #help-forum
Open in Discord
Eyebrowed ThrushOP
I am working with server actions in the nextjs. Lets suppose user had updated his name and action is completed using the server action. Then how i can update the user session in the same action instead of calling the auth update which refethes the data from the server again


I am using the next auth (beta version) sorry I forgot to add this in question.

And I think they don't provide a way to update session in server side

14 Replies

Look into using something like next-auth. In that library, the session can be set up as being a jwt token, which gets sent to the client via a cookie.

So essentially when you update the session on the serverside, you update that cookie, which then gets sent to the client via a call
Tan
You need to add a middleware for that
In the middleware you request your api get new sessions and set new cookies from the middleware
@Tan You need to add a middleware for that
You can also set cookies inside server action btw
@MikeTheTechLead You can also set cookies inside server action btw
Tan
Yup and it also can be called from middleware
https://nextjs.org/docs/app/api-reference/functions/cookies @Eyebrowed Thrush there's a little bit information here about it
Eyebrowed ThrushOP
I am using the next auth (beta version) sorry I forgot to add this in question.

And I think they don't provide a way to update session in server side
Ofc this is based on some assumption, since there's nothing "Auth" built-in in next so when you refer "Auth update" I'm not sure what you refer to
Ah
There is a getServerSession function which allows you to get session serverside in the non beta version. I'm guessing it's still there in the beta version
I am not sure if you can update through that
Eyebrowed ThrushOP
I think its read only
It doesn't allow to update
Things start getting confusing after they have switched to auth js from next auth.
New docs are a bit unclear