Easiest way to save state within client and server components
Unanswered
style posted this in #help-forum
styleOP
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
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
from a db?
@style 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
Turkish Van
Have You considered having it in the URL as
searchParam? Something like: ...?admin=true.@!=tgt How aare you getting information about admin mode?
styleOP
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`.
styleOP
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
Following that, You will have to make sure that user really is
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.