diff --git a/server/app/auth/signout/route.ts b/server/app/auth/signout/route.ts new file mode 100644 index 0000000..78a7e5c --- /dev/null +++ b/server/app/auth/signout/route.ts @@ -0,0 +1,35 @@ +import { cookies } from "next/headers"; +import { redirect } from "next/navigation"; +import { createServerClient, type CookieOptions } from "@supabase/ssr"; + +export async function GET() { + const cookieStore = cookies(); + + const supabase = createServerClient( + process.env.NEXT_PUBLIC_SUPABASE_URL!, + process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, + { + cookies: { + get(name: string) { + return cookieStore.get(name)?.value; + }, + set(name: string, value: string, options: CookieOptions) { + cookieStore.set({ name, value, ...options }); + }, + remove(name: string, options: CookieOptions) { + cookieStore.delete({ name, ...options }); + }, + }, + }, + ); + + const { + data: { session }, + } = await supabase.auth.getSession(); + + if (session) { + await supabase.auth.signOut(); + } + + redirect("/"); +} diff --git a/server/app/header.tsx b/server/app/header.tsx index 03d794e..a4aac12 100644 --- a/server/app/header.tsx +++ b/server/app/header.tsx @@ -4,21 +4,14 @@ import Link from "next/link"; import Image from "next/image"; import { Disclosure, Menu, Transition } from "@headlessui/react"; import { Bars3Icon, BellIcon, XMarkIcon } from "@heroicons/react/24/outline"; -import { createBrowserClient } from "@supabase/ssr"; import clsx from "clsx"; import beerTap from "@/public/beer-tap.png"; -// const user = { -// name: "Tom Cook", -// email: "tom@example.com", -// imageUrl: -// "https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80", -// }; const navigation = [{ name: "Dashboard", href: "/query", current: true }]; const userNavigation = [ { name: "Your Profile", href: "#" }, { name: "Settings", href: "#" }, - { name: "Sign out", href: "#" }, + { name: "Sign out", href: "/auth/signout" }, ]; export default function Header({ user }: { user: any }) { diff --git a/server/app/login/page.tsx b/server/app/login/page.tsx index 2140356..4a9f204 100644 --- a/server/app/login/page.tsx +++ b/server/app/login/page.tsx @@ -25,7 +25,7 @@ export default async function LoginPage() { } = await supabase.auth.getSession(); if (session) { - redirect("/dashboard"); + redirect("/query"); } return ( diff --git a/server/middleware.ts b/server/middleware.ts index 31b05f1..3ccd497 100644 --- a/server/middleware.ts +++ b/server/middleware.ts @@ -1,12 +1,12 @@ -import { createServerClient, type CookieOptions } from '@supabase/ssr' -import { NextResponse, type NextRequest } from 'next/server' +import { createServerClient, type CookieOptions } from "@supabase/ssr"; +import { NextResponse, type NextRequest } from "next/server"; export async function middleware(request: NextRequest) { let response = NextResponse.next({ request: { headers: request.headers, }, - }) + }); const supabase = createServerClient( process.env.NEXT_PUBLIC_SUPABASE_URL!, @@ -14,49 +14,58 @@ export async function middleware(request: NextRequest) { { cookies: { get(name: string) { - return request.cookies.get(name)?.value + return request.cookies.get(name)?.value; }, set(name: string, value: string, options: CookieOptions) { request.cookies.set({ name, value, ...options, - }) + }); response = NextResponse.next({ request: { headers: request.headers, }, - }) + }); response.cookies.set({ name, value, ...options, - }) + }); }, remove(name: string, options: CookieOptions) { request.cookies.set({ name, - value: '', + value: "", ...options, - }) + }); response = NextResponse.next({ request: { headers: request.headers, }, - }) + }); response.cookies.set({ name, - value: '', + value: "", ...options, - }) + }); }, }, - } - ) + }, + ); + + const { + data: { user }, + } = await supabase.auth.getUser(); - await supabase.auth.getUser() + if (!user && request.nextUrl.pathname !== "/") { + const url = request.nextUrl.clone(); - return response + url.pathname = `/404`; + return NextResponse.rewrite(url); + } + + return response; } export const config = { @@ -68,6 +77,6 @@ export const config = { * - favicon.ico (favicon file) * Feel free to modify this pattern to include more paths. */ - '/((?!_next/static|_next/image|favicon.ico).*)', + "/((?!_next/static|_next/image|favicon.ico).*)", ], -} +};