54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
import React, { useState } from 'react';
|
|
import { useNavigate } from 'react-router-dom';
|
|
import api from '../services/api';
|
|
import { Button, TextField, Container, Typography } from '@mui/material';
|
|
|
|
const Login: React.FC = () => {
|
|
const [username, setUsername] = useState('');
|
|
const [password, setPassword] = useState('');
|
|
const navigate = useNavigate();
|
|
|
|
const handleLogin = async () => {
|
|
try {
|
|
const response = await api.post('/token', new URLSearchParams({
|
|
username: username,
|
|
password: password,
|
|
}));
|
|
localStorage.setItem('token', response.data.access_token);
|
|
navigate('/');
|
|
} catch (error) {
|
|
console.error('Login failed', error);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Container maxWidth="xs">
|
|
<Typography variant="h4" component="h1" gutterBottom>
|
|
Login
|
|
</Typography>
|
|
<TextField
|
|
label="Username"
|
|
variant="outlined"
|
|
fullWidth
|
|
margin="normal"
|
|
value={username}
|
|
onChange={(e) => setUsername(e.target.value)}
|
|
/>
|
|
<TextField
|
|
label="Password"
|
|
type="password"
|
|
variant="outlined"
|
|
fullWidth
|
|
margin="normal"
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
/>
|
|
<Button variant="contained" color="primary" fullWidth onClick={handleLogin}>
|
|
Login
|
|
</Button>
|
|
</Container>
|
|
);
|
|
};
|
|
|
|
export default Login;
|