-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import axios from 'axios'; | ||
|
||
const BASE_URL = import.meta.env.VITE_AUTH_API_BASE_URL; | ||
const resource = '/api/auth'; | ||
const url = `${BASE_URL}${resource}`; | ||
|
||
export async function login(payload: { email: string; password: string }) { | ||
const endpoint = `${url}/auth/login`; | ||
return await axios.post(endpoint, payload); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { CircularProgress } from '@mui/material'; | ||
import Box from '@mui/material/Box'; | ||
import Button from '@mui/material/Button'; | ||
import Checkbox from '@mui/material/Checkbox'; | ||
import Container from '@mui/material/Container'; | ||
import CssBaseline from '@mui/material/CssBaseline'; | ||
import FormControlLabel from '@mui/material/FormControlLabel'; | ||
import Grid from '@mui/material/Grid'; | ||
import Link from '@mui/material/Link'; | ||
import { createTheme, ThemeProvider } from '@mui/material/styles'; | ||
import TextField from '@mui/material/TextField'; | ||
import Typography from '@mui/material/Typography'; | ||
import * as React from 'react'; | ||
import { login } from '../api/auth-api'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
// TODO remove, this demo shouldn't need to reset the theme. | ||
const defaultTheme = createTheme(); | ||
|
||
export default function Login() { | ||
const [error, setError] = React.useState(false); | ||
const [loading, setLoading] = React.useState(false); | ||
const navigate = useNavigate(); | ||
|
||
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
setLoading(true); | ||
setError(false); | ||
const data = new FormData(event.currentTarget); | ||
|
||
try { | ||
const res = await login({ | ||
email: data.get('email').toString(), | ||
password: data.get('password').toString(), | ||
}); | ||
const token = res.data?.access_token; | ||
console.log(token); | ||
navigate('/'); | ||
console.log('redirected'); | ||
} catch (err) { | ||
setError(true); | ||
console.log(err); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<ThemeProvider theme={defaultTheme}> | ||
<Container component="main" maxWidth="xs"> | ||
<CssBaseline /> | ||
<Box | ||
sx={{ | ||
marginTop: 8, | ||
display: 'flex', | ||
flexDirection: 'column', | ||
alignItems: 'center', | ||
}} | ||
> | ||
{/* <Avatar sx={{ m: 1, bgcolor: 'secondary.main' }}> | ||
<LockOutlinedIcon /> | ||
</Avatar> */} | ||
<Typography component="h1" variant="h5"> | ||
Welcome back | ||
</Typography> | ||
<Box component="form" onSubmit={handleSubmit} sx={{ mt: 1 }}> | ||
<TextField | ||
margin="normal" | ||
required | ||
fullWidth | ||
id="email" | ||
label="Email Address" | ||
name="email" | ||
autoComplete="email" | ||
autoFocus | ||
error={error} | ||
/> | ||
<TextField | ||
margin="normal" | ||
required | ||
fullWidth | ||
name="password" | ||
label="Password" | ||
type="password" | ||
id="password" | ||
autoComplete="current-password" | ||
error={error} | ||
helperText={error && 'Email or password is incorrect'} | ||
/> | ||
<FormControlLabel | ||
control={<Checkbox value="remember" color="primary" />} | ||
label="Remember me" | ||
/> | ||
<Button | ||
type="submit" | ||
fullWidth | ||
variant="contained" | ||
sx={{ mt: 3, mb: 2 }} | ||
> | ||
{loading ? ( | ||
<CircularProgress color="inherit" size={30} /> | ||
) : ( | ||
'Sign In' | ||
)} | ||
</Button> | ||
<Grid container> | ||
<Grid item xs> | ||
<Link href="#" variant="body2"> | ||
Forgot password? | ||
</Link> | ||
</Grid> | ||
<Grid item> | ||
<Link href="#" variant="body2"> | ||
{"Don't have an account? Sign Up"} | ||
</Link> | ||
</Grid> | ||
</Grid> | ||
</Box> | ||
</Box> | ||
</Container> | ||
</ThemeProvider> | ||
); | ||
} |