source: pages/auth/login/+Page.tsx@ b6e1b3c

main
Last change on this file since b6e1b3c was 1bf6e1f, checked in by Mihail <mihail2.naumov@…>, 7 months ago

Implemented part of the frontend (index, user, completedBuilds).

  • Property mode set to 100644
File size: 3.0 KB
Line 
1import React, { useState } from "react";
2import { Container, Box, Typography, TextField, Button, Paper, Alert } from "@mui/material";
3
4export default function LoginPage() {
5 const [error, setError] = useState<string | null>(null);
6
7 const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
8 event.preventDefault();
9 setError(null);
10
11 const formData = new FormData(event.currentTarget);
12 const username = formData.get("username");
13 const password = formData.get("password");
14
15 const res = await fetch("/api/auth/callback/credentials", {
16 method: "POST",
17 headers: { "Content-Type": "application/json" },
18 body: JSON.stringify({
19 username,
20 password,
21 csrfToken: await getCsrfToken(),
22 }),
23 });
24
25 if (res.url.includes("error")) {
26 setError("Invalid username or password");
27 } else {
28 window.location.href = "/";
29 }
30 };
31
32 return (
33 <Container component="main" maxWidth="xs">
34 <Box sx={{ marginTop: 8, display: "flex", flexDirection: "column", alignItems: "center" }}>
35 <Paper elevation={3} sx={{ p: 4, width: '100%', display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
36
37 <Typography component="h1" variant="h5">
38 Log in
39 </Typography>
40
41 {error && (
42 <Alert severity="error" sx={{ mt: 2, width: '100%' }}>
43 {error}
44 </Alert>
45 )}
46
47 <Box component="form" onSubmit={handleSubmit} sx={{ mt: 1, width: '100%' }}>
48 <TextField
49 margin="normal"
50 required
51 fullWidth
52 id="username"
53 label="Username"
54 name="username"
55 autoComplete="username"
56 autoFocus
57 />
58 <TextField
59 margin="normal"
60 required
61 fullWidth
62 name="password"
63 label="Password"
64 type="password"
65 id="password"
66 autoComplete="current-password"
67 />
68
69 <Button
70 type="submit"
71 fullWidth
72 variant="contained"
73 sx={{ mt: 3, mb: 2 }}
74 >
75 Sign In
76 </Button>
77 </Box>
78 </Paper>
79 </Box>
80 </Container>
81 );
82}
83
84async function getCsrfToken() {
85 const res = await fetch("/api/auth/csrf");
86 const data = await res.json();
87 return data.csrfToken;
88}
Note: See TracBrowser for help on using the repository browser.