I followed these docs https://appwrite.io/docs/tutorials/nextjs-ssr-auth/step-7 However, when having successfully logged in via google, I get immediately redirected to the signin. But, I should getting forwared to the profile page.
// // src/app/oauth/route.ts
import { config } from "@/lib/config";
import { createAdminClient } from "@/lib/server/appwrite";
import { cookies } from "next/headers";
import { NextRequest, NextResponse } from "next/server";
export async function GET(request: NextRequest) {
const userId = request.nextUrl.searchParams.get("userId");
const secret = request.nextUrl.searchParams.get("secret");
if (!userId || !secret) {
return new NextResponse("OAuth2 did not provide token", { status: 400 });
}
const { account } = await createAdminClient();
const session = await account.createSession({ userId, secret });
if (!session || !session.secret) {
return new NextResponse("Failed to create session from token", {
status: 400,
});
}
(await cookies()).set(config.appwrite.sessionName, session.secret, {
path: "/",
httpOnly: true,
sameSite: "strict",
expires: new Date(session.expire),
secure: true,
});
return NextResponse.redirect(`${request.nextUrl.origin}/profile`);
}
// src/app/(protected)/profile/layout.tsx
import Header from "@/components/Header";
import { getLoggedInUser } from "@/lib/server/appwrite";
import { redirect } from "next/navigation";
export default async function ProtectedLayout({
children,
}: {
children: React.ReactNode;
}) {
const user = await getLoggedInUser();
if (!user) {
redirect("/signin"); // --> THIS seems to kick in bevor the cookie has been set
}
return (
<div className="min-h-screen flex flex-col gap-y-8">
<Header />
<main className="container mx-auto max-w-[1400px]">{children}</main>
</div>
);
}
Not sure what I am doing wrong here
Recommended threads
- Sudden CORS Errors - Domain hasn't Chang...
I have an Appwrite project with two web apps configured, the first one has the hostname `*` and the second one I just added to test if it could fix the issue wi...
- Any way to temporarily bypass the email ...
Hey guys, any way to bypass the email verification to use the accounts again? i need to recover some projects that due to recent changes have been stopped, and ...
- Collections list not showing up when try...
I'm trying to create new relationship attribute but both one way and two way relationship is not showing up collections list to connect with my relationship att...