import React, { useState } from "react"; import { Container, Box, Typography, TextField, Button, Paper, Alert } from "@mui/material"; export default function LoginPage() { const [error, setError] = useState(null); const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); setError(null); const formData = new FormData(event.currentTarget); const username = formData.get("username"); const password = formData.get("password"); const res = await fetch("/api/auth/callback/credentials", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ username, password, csrfToken: await getCsrfToken(), }), }); if (res.url.includes("error")) { setError("Invalid username or password"); } else { window.location.href = "/"; } }; return ( Log in {error && ( {error} )} ); } async function getCsrfToken() { const res = await fetch("/api/auth/csrf"); const data = await res.json(); return data.csrfToken; }