using the uuid library, I want to generate a uuid (called sessionID) that can be referenced anywhere in my application (including functions that make database requests through prisma and actual pages of my app), and only updates under certain circumstances (ex. logging in). Is there a way to implement this? I've tried using getServerSideProps in the files that require sessionID but I haven't got it to work. Would greatly appreciate any help!
#Using a global variable throughout .tsx and .ts files
19 messages · Page 1 of 1 (latest)
🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord
🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize
✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)
a sessionID sounds like you need it in auth (give more context if that's not the case). Then you would save this sessionID inside your user cookies and this can be reference on server cookies() or clientside as well
yeah I was planning to initialize my sessionID in auth, is cookies() an external library?
cookies() is a function from nextjs itself. With that function you get access to the cookies from the current request
oh sick, I'll try it out soon and lyk how it goes
keep in mind the advantages and disadvantages of the specific auth method: https://velog.velcdn.com/images/devfish/post/6e8c7828-09d3-46c6-93cd-d683f31b0ae2/image.png
sorry for the long wait, but I tried implementing this and got this error when calling cookies().get("sessionID") in a "use client" page. Is there a way to work around this?
You should make a context provider in layout that gets the cookies and exposes it down stream.
Easiest and most flexible method to achieving what you want.
Do I just build it and wrap it around my things in layout.tsx?
I would lookup how to make a context provider there’s more than that but I’m in the pool and can’t type it up 😂
The cookies function is only available serverside. As Jbomcz said you can either use a context provider to get the value serverside and pass it to the client or you can read the cookies clientside as well with the help of additional packages like: https://www.npmjs.com/package/cookies-next
If you still want to use the context method, this might be what you looking for: https://www.npmjs.com/package/next-client-cookies
Of course you can also archive that functionality without using a library for that. Then take the second link as guide and just add the cookies to the context
having some confusion here with cookies-next:
when I try to set and get the cookie in the same file, getCookie() returns undefined; I know the sessionID exists, I just can't understand why its not being saved with setCookie or why it's being saved as undefined. Any clarity would be greatly appreciated
do you see in your browser cookies after you set it?
then that's the problem. To be honest I never worked with the package, so I cant help you with that