NextJS cookies not available client-side without a page refresh

I’m building an app using NextJS (app router system). I’m building the sign-up workflow, using NextAuth, and Resend. I’m using the newly available Resend provider from NextAuth.

Here is the workflow explained:

  1. Sign-up form to get the user’s email.
  2. When submitted, I’m calling a server action which triggers the signIn() method from NextAuth.
  3. This sends a custom email, with a validation code that I created. A verificationToken is automatically created in DB, using the NextAuth Adapter for Prisma.
  4. The user clicks on the validation button inside the email to be redirected to my validation page where he can input the validation code he received (an 6 digits OTP).
  5. When the form is submitted, it triggers a server action that checks if the code is correct and perform a redirect to the onboarding page.

My process works until the 5th step, where I’m being redirected to the onboarding page. I can see a session token created in my cookies, and I can also see that this line const session = await auth(); is storing my session object inside my root layout.tsx. Same for the console.log("Session in Navbar: ", session); inside my Navbar.tsx component.

However it seems that isLoggedIn is not evaluate to true inside my tsx component (Navbar), and I can see that the logs from terminal and the ones from the browser console are not the same (isLoggedin=true on server, and false on browser).

When I manually reload the onboarding page, using cmd+R, my Navbar is updating as I want.

my root layout.tsx

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import "./globals.css";
import { auth } from "../../auth";
import { cn } from "@/lib/utils";
import { Poppins } from "next/font/google";
import { Navbar } from "@/components/Navbar";
import { SessionProvider } from "next-auth/react";
import { Toaster } from "@/components/ui/toaster";
const fontHeading = Poppins({
subsets: ["latin"],
display: "swap",
variable: "--font-heading",
weight: ["100", "200", "300", "400", "500", "600", "700", "800", "900"],
});
const fontBody = Poppins({
subsets: ["latin"],
display: "swap",
variable: "--font-body",
weight: ["100", "200", "300", "400", "500", "600", "700", "800", "900"],
});
export default async function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
const session = await auth();
console.log("Session in Layout: ", session);
return (
<html lang="en">
<body
className={cn(
"antialiased",
fontHeading.variable,
fontBody.variable
)}
>
<SessionProvider session={session}>
<Navbar />
<section className="px-[5%]">{children}</section>
</SessionProvider>
<Toaster />
</body>
</html>
);
}
</code>
<code>import "./globals.css"; import { auth } from "../../auth"; import { cn } from "@/lib/utils"; import { Poppins } from "next/font/google"; import { Navbar } from "@/components/Navbar"; import { SessionProvider } from "next-auth/react"; import { Toaster } from "@/components/ui/toaster"; const fontHeading = Poppins({ subsets: ["latin"], display: "swap", variable: "--font-heading", weight: ["100", "200", "300", "400", "500", "600", "700", "800", "900"], }); const fontBody = Poppins({ subsets: ["latin"], display: "swap", variable: "--font-body", weight: ["100", "200", "300", "400", "500", "600", "700", "800", "900"], }); export default async function RootLayout({ children, }: Readonly<{ children: React.ReactNode; }>) { const session = await auth(); console.log("Session in Layout: ", session); return ( <html lang="en"> <body className={cn( "antialiased", fontHeading.variable, fontBody.variable )} > <SessionProvider session={session}> <Navbar /> <section className="px-[5%]">{children}</section> </SessionProvider> <Toaster /> </body> </html> ); } </code>
import "./globals.css";
import { auth } from "../../auth";
import { cn } from "@/lib/utils";
import { Poppins } from "next/font/google";
import { Navbar } from "@/components/Navbar";
import { SessionProvider } from "next-auth/react";
import { Toaster } from "@/components/ui/toaster";

const fontHeading = Poppins({
    subsets: ["latin"],
    display: "swap",
    variable: "--font-heading",
    weight: ["100", "200", "300", "400", "500", "600", "700", "800", "900"],
});

const fontBody = Poppins({
    subsets: ["latin"],
    display: "swap",
    variable: "--font-body",
    weight: ["100", "200", "300", "400", "500", "600", "700", "800", "900"],
});

export default async function RootLayout({
    children,
}: Readonly<{
    children: React.ReactNode;
}>) {
    const session = await auth();
    console.log("Session in Layout: ", session);

    return (
        <html lang="en">
            <body
                className={cn(
                    "antialiased",
                    fontHeading.variable,
                    fontBody.variable
                )}
            >
                <SessionProvider session={session}>
                    <Navbar />
                    <section className="px-[5%]">{children}</section>
                </SessionProvider>
                <Toaster />
            </body>
        </html>
    );
}

my Navbar component

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>"use client";
import Link from "next/link";
import { Sheet, SheetTrigger, SheetContent } from "@/components/ui/sheet";
import { NavLink, Button } from "@/components/ui/button";
import Logo from "./Logo";
import { Menu } from "lucide-react";
import { Separator } from "./ui/separator";
import { useEffect, useState } from "react";
import { signOut, useSession } from "next-auth/react";
export function Navbar() {
const { data: session, status, update } = useSession();
const [isSheetOpen, setSheetOpen] = useState(false);
const [isLoggedIn, setIsLoggedIn] = useState(false);
const handleCloseSheet = () => {
setSheetOpen(false);
};
useEffect(() => {
if (status === "authenticated") {
setIsLoggedIn(true);
update();
} else {
setIsLoggedIn(false);
}
}, [status, session]);
console.log("Session in Navbar: ", session);
console.log("Current time is: ", new Date().toLocaleTimeString());
return (
// I'm performing an if condition on the value of isLoggedIn to modify the UI of the Navbar here
</code>
<code>"use client"; import Link from "next/link"; import { Sheet, SheetTrigger, SheetContent } from "@/components/ui/sheet"; import { NavLink, Button } from "@/components/ui/button"; import Logo from "./Logo"; import { Menu } from "lucide-react"; import { Separator } from "./ui/separator"; import { useEffect, useState } from "react"; import { signOut, useSession } from "next-auth/react"; export function Navbar() { const { data: session, status, update } = useSession(); const [isSheetOpen, setSheetOpen] = useState(false); const [isLoggedIn, setIsLoggedIn] = useState(false); const handleCloseSheet = () => { setSheetOpen(false); }; useEffect(() => { if (status === "authenticated") { setIsLoggedIn(true); update(); } else { setIsLoggedIn(false); } }, [status, session]); console.log("Session in Navbar: ", session); console.log("Current time is: ", new Date().toLocaleTimeString()); return ( // I'm performing an if condition on the value of isLoggedIn to modify the UI of the Navbar here </code>
"use client";

import Link from "next/link";
import { Sheet, SheetTrigger, SheetContent } from "@/components/ui/sheet";
import { NavLink, Button } from "@/components/ui/button";
import Logo from "./Logo";
import { Menu } from "lucide-react";
import { Separator } from "./ui/separator";
import { useEffect, useState } from "react";
import { signOut, useSession } from "next-auth/react";

export function Navbar() {
    const { data: session, status, update } = useSession();

    const [isSheetOpen, setSheetOpen] = useState(false);
    const [isLoggedIn, setIsLoggedIn] = useState(false);

    const handleCloseSheet = () => {
        setSheetOpen(false);
    };
    useEffect(() => {
        if (status === "authenticated") {
            setIsLoggedIn(true);
            update();
        } else {
            setIsLoggedIn(false);
        }
    }, [status, session]);

    console.log("Session in Navbar: ", session);
    console.log("Current time is: ", new Date().toLocaleTimeString());

    return (
// I'm performing an if condition on the value of isLoggedIn to modify the UI of the Navbar here
  • I tried refresh the page using useRouter() hook inside my Navbar component.
  • I tried a window.location.reload() to simulate a CMD+R.
  • I tried using useEffect() inside the Navbar component.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật