Fix admin login: redirect path, add logout, remove unused code
- Fix redirect from /admin/login to /admin-login - Add DELETE endpoint for logout - Connect logout button to API - Remove unused admin state/localStorage Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
ffaa92cd13
commit
d94a15e38e
2 changed files with 25 additions and 16 deletions
|
|
@ -25,7 +25,6 @@ export default function AdminLayout({ children }: { children: React.ReactNode })
|
|||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const [sidebarOpen, setSidebarOpen] = useState(true);
|
||||
const [admin, setAdmin] = useState<{ username: string; role: string } | null>(null);
|
||||
|
||||
// Check if this is the login page - don't show sidebar
|
||||
const isLoginPage = pathname === "/admin-login";
|
||||
|
|
@ -36,19 +35,16 @@ export default function AdminLayout({ children }: { children: React.ReactNode })
|
|||
|
||||
const token = document.cookie.match(/tia_admin_session=([^;]+)/)?.[1];
|
||||
if (!token) {
|
||||
router.push("/admin/login");
|
||||
router.push("/admin-login");
|
||||
return;
|
||||
}
|
||||
const stored = localStorage.getItem("admin_user");
|
||||
if (stored) {
|
||||
setAdmin(JSON.parse(stored));
|
||||
}
|
||||
}, [router, isLoginPage]);
|
||||
|
||||
const handleLogout = () => {
|
||||
localStorage.removeItem("admin_token");
|
||||
localStorage.removeItem("admin_user");
|
||||
router.push("/admin/login");
|
||||
const handleLogout = async () => {
|
||||
try {
|
||||
await fetch("/api/admin/auth", { method: "DELETE" });
|
||||
} catch (e) {}
|
||||
router.push("/admin-login");
|
||||
};
|
||||
|
||||
// Login page - render without sidebar
|
||||
|
|
@ -98,12 +94,6 @@ export default function AdminLayout({ children }: { children: React.ReactNode })
|
|||
|
||||
{/* Footer */}
|
||||
<div className="mt-auto p-4 border-t border-gray-700">
|
||||
{sidebarOpen && admin && (
|
||||
<div className="mb-3">
|
||||
<div className="text-sm font-medium">{admin.username}</div>
|
||||
<div className="text-xs text-gray-400">{admin.role}</div>
|
||||
</div>
|
||||
)}
|
||||
<button
|
||||
onClick={handleLogout}
|
||||
className="w-full px-3 py-2 bg-gray-700 text-gray-400 hover:text-white rounded-lg text-sm"
|
||||
|
|
|
|||
|
|
@ -118,6 +118,25 @@ export async function POST(request: Request) {
|
|||
}
|
||||
}
|
||||
|
||||
// DELETE - Logout (clear session)
|
||||
export async function DELETE(request: Request) {
|
||||
try {
|
||||
const cookieStore = await cookies();
|
||||
const sessionToken = cookieStore.get("tia_admin_session")?.value;
|
||||
|
||||
if (sessionToken) {
|
||||
await sql`DELETE FROM admin_sessions WHERE session_token = ${sessionToken}`;
|
||||
}
|
||||
|
||||
const response = NextResponse.json({ success: true });
|
||||
response.cookies.set("tia_admin_session", "", { maxAge: 0, path: "/" });
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error("Admin logout error:", error);
|
||||
return NextResponse.json({ error: String(error) }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// GET - Check current session
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue