please help with authentication flow
Unanswered
Pteromalid wasp posted this in #help-forum
Pteromalid waspOP
i want to verify the cookie to make sure the session is still valid, i call my server action in page.jsx BUT if there's an error i can not delete the cookie...how am i supposed to manage this???
like page.jsx:
check for existence of session token
if exists, call verifySession server action
can't delete the cookie in this function, can't return an error and delete the cookie in page.jsx (WHAT DO I DO???)
if valid, fetch data, pass to client wrapper or straight to client component
i don't want to rely on middleware to verify the token because i also need to know if i'm deleting it so i can notify the user that they are being logged out. this is so frustrating and it feels like it's difficult for no reason
Edit: I guess I can pass the verify session error to like a client wrapper and delete the cookie in that but seems like a janky workaround
like page.jsx:
check for existence of session token
if exists, call verifySession server action
can't delete the cookie in this function, can't return an error and delete the cookie in page.jsx (WHAT DO I DO???)
if valid, fetch data, pass to client wrapper or straight to client component
i don't want to rely on middleware to verify the token because i also need to know if i'm deleting it so i can notify the user that they are being logged out. this is so frustrating and it feels like it's difficult for no reason
Edit: I guess I can pass the verify session error to like a client wrapper and delete the cookie in that but seems like a janky workaround
3 Replies
@Pteromalid wasp i want to verify the cookie to make sure the session is still valid, i call my server action in page.jsx BUT if there's an error i can not delete the cookie...how am i supposed to manage this???
like page.jsx:
check for existence of session token
if exists, call verifySession server action
can't delete the cookie in this function, can't return an error and delete the cookie in page.jsx (WHAT DO I DO???)
if valid, fetch data, pass to client wrapper or straight to client component
i don't want to rely on middleware to verify the token because i also need to know if i'm deleting it so i can notify the user that they are being logged out. this is so frustrating and it feels like it's difficult for no reason
Edit: I guess I can pass the verify session error to like a client wrapper and delete the cookie in that but seems like a janky workaround
West African Crocodile
I think the token can only be deleted from a client component or by calling a server action or route handler from a client component. I searched for a way to do it at the beginning of page.tsx but couldn't find a valid one.
Yea, cookies are a client side feature, you can’t modify cookies in the server environment because they’re a Browser API
You can delete them in Server Actions but the call needs to be done from the client