Using a global variable throughout .tsx and .ts files
Unanswered
Polar bear posted this in #help-forum
Polar bearOP
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!
18 Replies
@Polar bear 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!
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@B33fb0n3 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
Polar bearOP
yeah I was planning to initialize my sessionID in auth, is cookies() an external library?
@Polar bear yeah I was planning to initialize my sessionID in auth, is cookies() an external library?
[cookies()](https://nextjs.org/docs/app/api-reference/functions/cookies) is a function from nextjs itself. With that function you get access to the cookies from the current request
Polar bearOP
oh sick, I'll try it out soon and lyk how it goes
@Polar bear 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
@B33fb0n3 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
Polar bearOP
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.
@Jboncz You should make a context provider in layout that gets the cookies and exposes it down stream.
Polar bearOP
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 😂
@Polar bear 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?
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
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
Polar bearOP
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
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
@Polar bear 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?
Polar bearOP
nope
unless I'm missing smth
then that's the problem. To be honest I never worked with the package, so I cant help you with that
Polar bearOP
Might just try context atp