Authentication using server actions
Answered
sohunn posted this in #help-forum
sohunnOP
Hey! I have a form in
What I mean is, the
/login that takes in an email and a password. After submitting the form, the action validates the data, sets cookies and redirects the user to the /home route. If there are any form validation errors, I use the useActionState hook to show it in the UI. My question is, upon successful authentication, how do I update my UI to reflect information when they are redirected to the /home route? What I mean is, the
/home route looks different when a user is logged in and when a user isn’t logged in. So when my action completes, how do I let my UI know about this?20 Replies
I usually do this, there'd be a small chance that session might have expired
if cookies exist, you can basically show the differnt ui
@Anay-208 I usually do this, there'd be a small chance that session might have expired
sohunnOP
How do you end up handling expired sessions? Does your session ID and cookie have the same life?
Better way to put it, are your sessions valid as long as the cookie is valid?
@sohunn Better way to put it, are your sessions valid as long as the cookie is valid?
You can make it like that
you can add a maxAge param to a cookie
maxAge accepts a value in seconds
@Anay-208 you can add a maxAge param to a cookie
sohunnOP
Gotcha, what if the home page itself is a client component or has client components inside it?
Accessing cookies might not be possible right?
Accessing cookies might not be possible right?
You can try it out. For me, I was able to access key, even from server components.
@Anay-208 Why won't it be possible?
sohunnOP
Cuz you cannot access http cookies using client side js
Assuming my home page is a client component
That relies on effects and states
Umm, can you try it out? As I never faced this
sohunnOP
I’ll give it a shot, I’m pretty new to how next js 14 works, coming from sveltekit haha!
Thanks tho!
@Anay-208 You can check if the cookie exists
Mark this message as a solution