diff --git a/app/api/logout/route.ts b/app/api/logout/route.ts new file mode 100644 index 0000000..e3c60e1 --- /dev/null +++ b/app/api/logout/route.ts @@ -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; +} diff --git a/app/layout.tsx b/app/layout.tsx index 3f5ef78..bac882a 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -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 }) {session ? ( - - {userName} - +
+ + {userName} + + +
) : (
({})); + + if (!res.ok) { + alert(data.error || LOGOUT_FAILED_TEXT); + return; + } + + router.replace("/"); + router.refresh(); + } finally { + setLoading(false); + } + } + + return ( + + ); +}