feat: add admin sign out
This commit is contained in:
1 parent
b1ab7b4c0c
commit
98d610271f
2 files changed
+57
-1
No files matched your search
@@ -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;
|
||||||
|
}
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
|
import { Button } from '@/components/ui/Button';
|
||||||
import { Icon } from '@/components/ui/Icon';
|
import { Icon } from '@/components/ui/Icon';
|
||||||
import { cn } from '@/lib/utils';
|
import { cn } from '@/lib/utils';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { usePathname } from 'next/navigation';
|
import { usePathname, useRouter } from 'next/navigation';
|
||||||
import React, { useEffect, useState } from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
|
|
||||||
interface SidebarProps {
|
interface SidebarProps {
|
||||||
@@ -16,6 +17,9 @@ export const Sidebar: React.FC<SidebarProps> = ({
|
|||||||
onClose,
|
onClose,
|
||||||
}) => {
|
}) => {
|
||||||
const pathname = usePathname();
|
const pathname = usePathname();
|
||||||
|
const router = useRouter();
|
||||||
|
const [isSigningOut, setIsSigningOut] = useState(false);
|
||||||
|
const [signOutError, setSignOutError] = useState('');
|
||||||
|
|
||||||
const navigation = [
|
const navigation = [
|
||||||
{
|
{
|
||||||
@@ -56,6 +60,23 @@ export const Sidebar: React.FC<SidebarProps> = ({
|
|||||||
if (activeGroup) setOpenGroup(activeGroup);
|
if (activeGroup) setOpenGroup(activeGroup);
|
||||||
}, [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 (
|
return (
|
||||||
<>
|
<>
|
||||||
{/* Mobile Backdrop */}
|
{/* Mobile Backdrop */}
|
||||||
@@ -168,6 +189,25 @@ export const Sidebar: React.FC<SidebarProps> = ({
|
|||||||
<span>Auth Handshake</span>
|
<span>Auth Handshake</span>
|
||||||
<span className="text-[var(--text-muted)]">JWT</span>
|
<span className="text-[var(--text-muted)]">JWT</span>
|
||||||
</div>
|
</div>
|
||||||
|
{pathname !== '/vcon/setup' && (
|
||||||
|
<>
|
||||||
|
{signOutError && (
|
||||||
|
<p role="alert" className="text-[var(--danger)]">{signOutError}</p>
|
||||||
|
)}
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
icon="LogOut"
|
||||||
|
loading={isSigningOut}
|
||||||
|
disabled={isSigningOut}
|
||||||
|
onClick={signOut}
|
||||||
|
className="mt-1 w-full justify-start px-2 text-[11px]"
|
||||||
|
>
|
||||||
|
Sign out
|
||||||
|
</Button>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</aside>
|
</aside>
|
||||||
</>
|
</>
|
||||||
|
|||||||
Reference in new issue
Block a user