From 7f2b548eeee022aad399027e2f69dc34290e50a1 Mon Sep 17 00:00:00 2001 From: Jared Pereira Date: Wed, 28 May 2025 18:01:38 -0400 Subject: [PATCH] use regular route to logout --- actions/logout.ts | 9 --------- app/api/auth/logout/route.ts | 28 ++++++++++++++++++++++++++++ app/home/AccountSettings.tsx | 5 ++--- app/home/page.tsx | 4 +--- app/login/page.tsx | 23 ----------------------- src/auth.ts | 3 ++- 6 files changed, 33 insertions(+), 39 deletions(-) delete mode 100644 actions/logout.ts create mode 100644 app/api/auth/logout/route.ts delete mode 100644 app/login/page.tsx diff --git a/actions/logout.ts b/actions/logout.ts deleted file mode 100644 index bec8eaa5..00000000 --- a/actions/logout.ts +++ /dev/null @@ -1,9 +0,0 @@ -"use server"; - -import { cookies } from "next/headers"; -import { removeAuthToken } from "src/auth"; - -export async function logout() { - await removeAuthToken(); - (await cookies()).delete("identity"); -} diff --git a/app/api/auth/logout/route.ts b/app/api/auth/logout/route.ts new file mode 100644 index 00000000..5114fa4b --- /dev/null +++ b/app/api/auth/logout/route.ts @@ -0,0 +1,28 @@ +import { NextRequest } from "next/server"; +export const runtime = "edge"; +export const preferredRegion = []; + +export async function GET(req: NextRequest) { + const host = req.headers.get("host"); + const response = new Response("Logged out successfully", { + status: 200, + headers: { + "Content-Type": "text/plain", + }, + }); + + // Get the base domain from the host + const domain = host?.includes(":") ? host.split(":")[0] : host; + + // Clear the auth_token cookie on both the base domain and the domain with a leading dot + response.headers.append( + "Set-Cookie", + `auth_token=; Path=/; Domain=${domain}; Max-Age=0; HttpOnly; Secure; SameSite=Strict`, + ); + response.headers.append( + "Set-Cookie", + `auth_token=; Path=/; Domain=.${domain}; Max-Age=0; HttpOnly; Secure; SameSite=Strict`, + ); + + return response; +} diff --git a/app/home/AccountSettings.tsx b/app/home/AccountSettings.tsx index d71954ef..d6f30762 100644 --- a/app/home/AccountSettings.tsx +++ b/app/home/AccountSettings.tsx @@ -2,7 +2,6 @@ import { ActionButton } from "components/ActionBar/ActionButton"; import { Menu, MenuItem } from "components/Layout"; -import { logout } from "actions/logout"; import { mutate } from "swr"; import { AccountSmall } from "components/Icons/AccountSmall"; import { LogoutSmall } from "components/Icons/LogoutSmall"; @@ -16,8 +15,8 @@ export const AccountSettings = () => { > { - await logout(); - mutate("identity"); + await fetch("/api/auth/logout"); + mutate("identity", null); }} > diff --git a/app/home/page.tsx b/app/home/page.tsx index 4eeeda5d..d9051cff 100644 --- a/app/home/page.tsx +++ b/app/home/page.tsx @@ -21,9 +21,7 @@ import { supabaseServerClient } from "supabase/serverClient"; export default async function Home() { let cookieStore = await cookies(); - - let auth_token = cookieStore.get("auth_token")?.value; - let auth_res = auth_token ? await getIdentityData() : null; + let auth_res = await getIdentityData(); let identity: string | undefined; if (auth_res) identity = auth_res.id; else identity = cookieStore.get("identity")?.value; diff --git a/app/login/page.tsx b/app/login/page.tsx deleted file mode 100644 index 1106c765..00000000 --- a/app/login/page.tsx +++ /dev/null @@ -1,23 +0,0 @@ -import { cookies } from "next/headers"; -import LoginForm from "./LoginForm"; -import { logout } from "actions/logout"; - -export default async function LoginPage() { - let cookieStore = await cookies(); - let identity = cookieStore.get("auth_token")?.value; - if (!identity) - return ( -
- this is a login page! - -
- ); - return ( -
- identity: {identity} -
- -
-
- ); -} diff --git a/src/auth.ts b/src/auth.ts index 4f81ba96..37a57006 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -3,10 +3,11 @@ import { isProductionDomain } from "./utils/isProductionDeployment"; export async function setAuthToken(tokenID: string) { let c = await cookies(); + let host = (await headers()).get("host"); c.set("auth_token", tokenID, { maxAge: 60 * 60 * 24 * 365, secure: process.env.NODE_ENV === "production", - domain: isProductionDomain() ? "leaflet.pub" : undefined, + domain: isProductionDomain() ? host! : undefined, httpOnly: true, sameSite: "lax", }); -- 2.51.2