Next.js Discord

Discord Forum

Cookies not accessible

Unanswered
Short mackerel posted this in #help-forum
Open in Discord
Short mackerelOP
I am using appwrite ssr authentication. I am not able to access the cookie on my redirected page initially. The route handler which sets the cookie logs the cookie properly but when its redirected to /dashboard it shows there are no cookies on log. After a refresh it works properly but its a problem for me since i am checking for the logged in user on my dashboard page

// src/app/oauth/route.js
import { cookies } from "next/headers";
import { NextRequest, NextResponse } from "next/server";

import { createAdminClient } from "@/lib/server/appwrite";

export async function GET(request: NextRequest) {
    const userId = request.nextUrl.searchParams.get("userId");
    const secret = request.nextUrl.searchParams.get("secret");

    if (!userId || !secret) {
        return NextResponse.redirect("/auth/signup");
    }

    const { account } = await createAdminClient();
    const session = await account.createSession(userId, secret);

    cookies().set("my-custom-session", session.secret, {
        path: "/",
        httpOnly: true,
        sameSite: "strict",
        secure: true,
    });

console.log(cookies().get("my-custom-session")) // shows the cookie

    return NextResponse.redirect(`${request.nextUrl.origin}/dashboard`);
}


// dashboard/page.tsx
    // Log cookies to check availability
    console.log("Cookies on /dashboard:", cookies().get("my-custom-session")); // undefined

    const user = await getLoggedInUser();
    if (!user) redirect("/auth/login");

    const { email, name, emailVerification, $createdAt } = user;

0 Replies