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 router = useRouter();
|
||||||
const pathname = usePathname();
|
const pathname = usePathname();
|
||||||
const [sidebarOpen, setSidebarOpen] = useState(true);
|
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
|
// Check if this is the login page - don't show sidebar
|
||||||
const isLoginPage = pathname === "/admin-login";
|
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];
|
const token = document.cookie.match(/tia_admin_session=([^;]+)/)?.[1];
|
||||||
if (!token) {
|
if (!token) {
|
||||||
router.push("/admin/login");
|
router.push("/admin-login");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const stored = localStorage.getItem("admin_user");
|
|
||||||
if (stored) {
|
|
||||||
setAdmin(JSON.parse(stored));
|
|
||||||
}
|
|
||||||
}, [router, isLoginPage]);
|
}, [router, isLoginPage]);
|
||||||
|
|
||||||
const handleLogout = () => {
|
const handleLogout = async () => {
|
||||||
localStorage.removeItem("admin_token");
|
try {
|
||||||
localStorage.removeItem("admin_user");
|
await fetch("/api/admin/auth", { method: "DELETE" });
|
||||||
router.push("/admin/login");
|
} catch (e) {}
|
||||||
|
router.push("/admin-login");
|
||||||
};
|
};
|
||||||
|
|
||||||
// Login page - render without sidebar
|
// Login page - render without sidebar
|
||||||
|
|
@ -98,12 +94,6 @@ export default function AdminLayout({ children }: { children: React.ReactNode })
|
||||||
|
|
||||||
{/* Footer */}
|
{/* Footer */}
|
||||||
<div className="mt-auto p-4 border-t border-gray-700">
|
<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
|
<button
|
||||||
onClick={handleLogout}
|
onClick={handleLogout}
|
||||||
className="w-full px-3 py-2 bg-gray-700 text-gray-400 hover:text-white rounded-lg text-sm"
|
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
|
// GET - Check current session
|
||||||
export async function GET(request: Request) {
|
export async function GET(request: Request) {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue