diff --git a/app/api/auth/logout/route.ts b/app/api/auth/logout/route.ts new file mode 100644 index 0000000..c73dbce --- /dev/null +++ b/app/api/auth/logout/route.ts @@ -0,0 +1,16 @@ +import { NextResponse } from "next/server"; + +const SESSION_COOKIE = "datastack_admin_session"; + +export async function POST() { + const response = NextResponse.json({ success: true }); + response.cookies.set(SESSION_COOKIE, "", { + httpOnly: true, + secure: process.env.NODE_ENV === "production", + sameSite: "strict", + path: "/", + maxAge: 0, + expires: new Date(0), + }); + return response; +} diff --git a/components/layout/Sidebar.tsx b/components/layout/Sidebar.tsx index 1cad856..91d9fb1 100644 --- a/components/layout/Sidebar.tsx +++ b/components/layout/Sidebar.tsx @@ -1,9 +1,10 @@ 'use client'; +import { Button } from '@/components/ui/Button'; import { Icon } from '@/components/ui/Icon'; import { cn } from '@/lib/utils'; import Link from 'next/link'; -import { usePathname } from 'next/navigation'; +import { usePathname, useRouter } from 'next/navigation'; import React, { useEffect, useState } from 'react'; interface SidebarProps { @@ -16,6 +17,9 @@ export const Sidebar: React.FC = ({ onClose, }) => { const pathname = usePathname(); + const router = useRouter(); + const [isSigningOut, setIsSigningOut] = useState(false); + const [signOutError, setSignOutError] = useState(''); const navigation = [ { @@ -56,6 +60,23 @@ export const Sidebar: React.FC = ({ if (activeGroup) setOpenGroup(activeGroup); }, [activeGroup]); + const signOut = async () => { + setIsSigningOut(true); + setSignOutError(''); + try { + const response = await fetch('/api/auth/logout', { method: 'POST' }); + if (!response.ok) throw new Error('Unable to sign out. Please try again.'); + router.replace('/vcon/login'); + router.refresh(); + } catch (error) { + setSignOutError( + error instanceof Error ? error.message : 'Unable to sign out. Please try again.', + ); + } finally { + setIsSigningOut(false); + } + }; + return ( <> {/* Mobile Backdrop */} @@ -168,6 +189,25 @@ export const Sidebar: React.FC = ({ Auth Handshake JWT + {pathname !== '/vcon/setup' && ( + <> + {signOutError && ( +

{signOutError}

+ )} + + + )}