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
- is `account.get()` safe to be used in th...
I want to user's `id` for authentication. However, a while ago I was told in this server not to use `account.get()` and instead add user preferences for that us...
- Usage of the new Client() and dealing wi...
Hey guys, just a quick one - we had some web traffic the other day and it ended up bombing out - To put in perspective of how the app works, we have a Nuxt Ap...
- CORS errors in Obsidian custom plugin
Hi, anyone here familiar with obsidian community plugins? In short: it's a local first note app which supports writing your own add-ons / plugin But I keep get...