#Local Storage + Cookie

26 messages · Page 1 of 1 (latest)

rugged domeBOT
#

🔎 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)

royal tiger
#
royal stratus
#

I wouldn't do that, instead just read the cookie server side and return a value shouldShow if the cookies persist

compact vale
# royal tiger https://www.npmjs.com/package/cookies-next
"use client"

import {getAuth} from "@/app/actions/authAction";
import {getCookie} from 'cookies-next';


export default function Menu() {

    const cookie = getCookie("bestCookieEver");
    console.log(cookie);


    return  (
        <>

            <div>
                <p>Name: {cookie}</p>
                <p>Value: {cookie}</p>
            </div>
        </>
    )
}
``` not working cookie is undefined
royal stratus
compact vale
#

yes it is what does that mean?

royal stratus
compact vale
#

okay how would i do that

#

my usecase is i want to display in the navbar a profile button when the cookie is present im logged in if not redirect /login

royal stratus
compact vale
#

thats the problem my navbar is in my layout.tsx

#

if i do that i get an infinite loop

#
export default function RootLayout({children}: RootLayoutProps) {

    return (
        <>
            <html lang="en" suppressHydrationWarning>
            <body
                className={cn(
                    "min-h-screen bg-background font-sans antialiased",
                    fontSans.className
                )}
            >
            <ThemeProvider
                attribute="class"
                defaultTheme="system"
                enableSystem
                disableTransitionOnChange
            >
                <div className="relative flex min-h-screen flex-col bg-background">
                    <SiteHeader/>
                    <main className="flex-1">{children}</main>
                </div>
                <TailwindIndicator/>
            </ThemeProvider>
            </body>
            </html>
        </>
    )
}
#

its better to extract the siteheader there and put it on every page?

#

because the header isnt static

royal stratus
#

is the page going to static or dynamic?

compact vale
#

if i check the existence of the cookie and if not redirect to /login in the site header

compact vale
#

well maybe someone got an idea how can i solve my issue i do anything which makes it work xD

royal stratus
# compact vale will be an application to setup a discord bot - so dynamic i guess
import { cookies } from 'next/headers' 

export default function RootLayout({children}: RootLayoutProps) {
    const shouldShow = Boolean(cookies("bestCookieEver")?.value)
    return (
        <>
            <html lang="en" suppressHydrationWarning>
            <body
                className={cn(
                    "min-h-screen bg-background font-sans antialiased",
                    fontSans.className
                )}
            >
            <ThemeProvider
                attribute="class"
                defaultTheme="system"
                enableSystem
                disableTransitionOnChange
            >
                <div className="relative flex min-h-screen flex-col bg-background">
                    <SiteHeader shouldShow={shouldShow} />
                    <main className="flex-1">{children}</main>
                </div>
                <TailwindIndicator/>
            </ThemeProvider>
            </body>
            </html>
        </>
    )
}
compact vale
#

but if the cookie got deleted will this rerender my page?

compact vale
#

okay ill try it oput thanks