Add logout flow and header sign-out button

This commit is contained in:
爱喝水的木子
2026-03-20 12:50:44 +08:00
parent 33fbf38820
commit e6788d0e8f
3 changed files with 67 additions and 3 deletions

16
app/api/logout/route.ts Normal file
View File

@@ -0,0 +1,16 @@
import { NextResponse } from "next/server";
import { cookieName } from "@/lib/auth";
export async function POST() {
const res = NextResponse.json({ ok: true });
res.cookies.set(cookieName, "", {
httpOnly: true,
sameSite: "lax",
secure: process.env.NODE_ENV === "production",
maxAge: 0,
expires: new Date(0),
path: "/"
});
return res;
}

View File

@@ -3,6 +3,7 @@ import type { Metadata } from "next";
import Link from "next/link";
import { ReactNode } from "react";
import { cookies } from "next/headers";
import { LogoutButton } from "@/components/LogoutButton";
import { cookieName, verifySession } from "@/lib/auth";
export const metadata: Metadata = {
@@ -44,9 +45,12 @@ export default async function RootLayout({ children }: { children: ReactNode })
</nav>
{session ? (
<span className="rounded-full bg-slate-100 px-3 py-1 text-xs font-medium text-slate-700">
{userName}
</span>
<div className="flex items-center gap-2">
<span className="rounded-full bg-slate-100 px-3 py-1 text-xs font-medium text-slate-700">
{userName}
</span>
<LogoutButton />
</div>
) : (
<div className="flex items-center gap-2">
<Link