Using cookies in client-components #54906
-
|
I have a basic web app with login and home pages. I get the user's token after they log in and save it to the cookie store. I can get the token from server components and pass it down to the client component. However, I'm looking for a more convenient solution like using hooks or something else. "use server"
import {cookies} from 'next/headers'
export const getToken = (): string | undefined => {
return cookies().get('token')?.value;
}Basically, I want to determine if the user is logged in or not and use this information in my NavBar (client component) to show the Login or Logout button. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
|
For client-side cookies, you can implement third-party modules like js-cookie and will work like a charm. |
Beta Was this translation helpful? Give feedback.
-
|
I made a library. Would you like to try it? |
Beta Was this translation helpful? Give feedback.
For client-side cookies, you can implement third-party modules like js-cookie and will work like a charm.