Next.js Discord

Discord Forum

Easiest way to save state within client and server components

Unanswered
style posted this in #help-forum
Open in Discord
Hey, what im trying to achieve is have a client component that will contain button with text "admin Mode", it will change state to true/false, and then in my component i want to retrieve that value and use it in function. I would like to prevent adding too much code, so please tell me if it's even possible and whats the easiest way.

I tried usiung next-cookies and localStorage, but ofc none of them worked as cookies are mostly server-side and localStorage is client side and i need both

8 Replies

@!=tgt How aare you getting information about admin mode?
yeah, however i got button that shows if the user is admin - if they click it they go in to admin mode
@Turkish Van Have You considered having it in the URL as `searchParam`? Something like: `...?admin=true`.
yeah i thought of that, but im not sure if it's the easiest way, i would probably have to go through all <a>'s and add search param to it am i right?
and also check if user is indeed admin
@style and also check if user is indeed admin
Turkish Van
You could do that inside of middleware. In case user is not admin, he can just be redirected to default URL without that param inside of searchParams that is supposed to give him admin privileges inside of UI.
Or store it inside of cookies and read cookies inside of those functions that use that information, as You stated above.

Following that, You will have to make sure that user really is admin since cookies can be edited by the user.