Sign-in page
A sign-in card with email and password checks, a reset-password dialog and social buttons.
A sign-in card with email and password checks, a reset-password dialog and social buttons.
npm install @mui/icons-material @mui/material
First time with Material UI? Set it up once.
/**
* Sign-in template from Material UI's free templates, https://mui.com/material-ui/getting-started/templates/sign-in/
* MIT License, Copyright (c) 2014 Call-Em-All. Full text: https://github.com/mui/material-ui/blob/master/LICENSE
* Changed for Tastebud: uses MUI's default theme from the setup instead of the template's AppTheme and
* colour-mode switch, so it needs nothing beyond @mui/material; the Google and Facebook buttons use one-colour icons
* from @mui/icons-material instead of the brand-coloured logos; no Sitemark logo, full-window height, page gradient
* or autofocus (the form sits inside a page); the link at the bottom points to #.
*/
'use client';
import * as React from 'react';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import Checkbox from '@mui/material/Checkbox';
import FormControlLabel from '@mui/material/FormControlLabel';
import Divider from '@mui/material/Divider';
import FormLabel from '@mui/material/FormLabel';
import FormControl from '@mui/material/FormControl';
import Link from '@mui/material/Link';
import TextField from '@mui/material/TextField';
import Typography from '@mui/material/Typography';
import Stack from '@mui/material/Stack';
import MuiCard from '@mui/material/Card';
import { styled } from '@mui/material/styles';
import ForgotPassword from '@/components/ui/forgot-password';
import GoogleIcon from '@mui/icons-material/Google';
import FacebookIcon from '@mui/icons-material/Facebook';
const Card = styled(MuiCard)(({ theme }) => ({
display: 'flex',
flexDirection: 'column',
alignSelf: 'center',
width: '100%',
padding: theme.spacing(4),
gap: theme.spacing(2),
margin: 'auto',
[theme.breakpoints.up('sm')]: {
maxWidth: '450px',
},
boxShadow:
'hsla(220, 30%, 5%, 0.05) 0px 5px 15px 0px, hsla(220, 25%, 10%, 0.05) 0px 15px 35px -5px',
...theme.applyStyles('dark', {
boxShadow:
'hsla(220, 30%, 5%, 0.5) 0px 5px 15px 0px, hsla(220, 25%, 10%, 0.08) 0px 15px 35px -5px',
}),
}));
const SignInContainer = styled(Stack)(({ theme }) => ({
width: '100%',
padding: theme.spacing(2),
[theme.breakpoints.up('sm')]: {
padding: theme.spacing(4),
},
}));
export default function SignIn() {
const [emailError, setEmailError] = React.useState(false);
const [emailErrorMessage, setEmailErrorMessage] = React.useState('');
const [passwordError, setPasswordError] = React.useState(false);
const [passwordErrorMessage, setPasswordErrorMessage] = React.useState('');
const [open, setOpen] = React.useState(false);
const handleClickOpen = () => {
setOpen(true);
};
const handleClose = () => {
setOpen(false);
};
const handleSubmit = (event: React.SubmitEvent<HTMLFormElement>) => {
if (emailError || passwordError) {
event.preventDefault();
return;
}
const data = new FormData(event.currentTarget);
console.log({
email: data.get('email'),
password: data.get('password'),
});
};
const validateInputs = () => {
const email = document.getElementById('email') as HTMLInputElement;
const password = document.getElementById('password') as HTMLInputElement;
let isValid = true;
if (!email.value || !/\S+@\S+\.\S+/.test(email.value)) {
setEmailError(true);
setEmailErrorMessage('Please enter a valid email address.');
isValid = false;
} else {
setEmailError(false);
setEmailErrorMessage('');
}
if (!password.value || password.value.length < 6) {
setPasswordError(true);
setPasswordErrorMessage('Password must be at least 6 characters long.');
isValid = false;
} else {
setPasswordError(false);
setPasswordErrorMessage('');
}
return isValid;
};
return (
<>
<SignInContainer direction="column" sx={{ justifyContent: 'space-between' }}>
<Card variant="outlined">
<Typography
component="h1"
variant="h4"
sx={{ width: '100%', fontSize: 'clamp(2rem, 10vw, 2.15rem)' }}
>
Sign in
</Typography>
<Box
component="form"
onSubmit={handleSubmit}
noValidate
sx={{
display: 'flex',
flexDirection: 'column',
width: '100%',
gap: 2,
}}
>
<FormControl>
<FormLabel htmlFor="email">Email</FormLabel>
<TextField
error={emailError}
helperText={emailErrorMessage}
id="email"
type="email"
name="email"
placeholder="your@email.com"
autoComplete="email"
required
fullWidth
variant="outlined"
color={emailError ? 'error' : 'primary'}
/>
</FormControl>
<FormControl>
<FormLabel htmlFor="password">Password</FormLabel>
<TextField
error={passwordError}
helperText={passwordErrorMessage}
name="password"
placeholder="••••••"
type="password"
id="password"
autoComplete="current-password"
required
fullWidth
variant="outlined"
color={passwordError ? 'error' : 'primary'}
/>
</FormControl>
<FormControlLabel
control={<Checkbox value="remember" color="primary" />}
label="Remember me"
/>
<ForgotPassword open={open} handleClose={handleClose} />
<Button
type="submit"
fullWidth
variant="contained"
onClick={validateInputs}
>
Sign in
</Button>
<Link
component="button"
type="button"
onClick={handleClickOpen}
variant="body2"
sx={{ alignSelf: 'center' }}
>
Forgot your password?
</Link>
</Box>
<Divider>or</Divider>
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
<Button
fullWidth
variant="outlined"
onClick={() => alert('Sign in with Google')}
startIcon={<GoogleIcon />}
>
Sign in with Google
</Button>
<Button
fullWidth
variant="outlined"
onClick={() => alert('Sign in with Facebook')}
startIcon={<FacebookIcon />}
>
Sign in with Facebook
</Button>
<Typography sx={{ textAlign: 'center' }}>
Don't have an account?{' '}
<Link
href="#"
variant="body2"
sx={{ alignSelf: 'center' }}
>
Sign up
</Link>
</Typography>
</Box>
</Card>
</SignInContainer>
</>
);
}
/**
* Component from Material UI's free sign-in template, https://mui.com/material-ui/getting-started/templates/sign-in/
* MIT License, Copyright (c) 2014 Call-Em-All. Full text: https://github.com/mui/material-ui/blob/master/LICENSE
*/
'use client';
import * as React from 'react';
import Button from '@mui/material/Button';
import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
import DialogContentText from '@mui/material/DialogContentText';
import DialogTitle from '@mui/material/DialogTitle';
import OutlinedInput from '@mui/material/OutlinedInput';
interface ForgotPasswordProps {
open: boolean;
handleClose: () => void;
}
export default function ForgotPassword({ open, handleClose }: ForgotPasswordProps) {
return (
<Dialog
open={open}
onClose={handleClose}
slotProps={{
paper: {
component: 'form',
onSubmit: (event: React.SubmitEvent<HTMLFormElement>) => {
event.preventDefault();
handleClose();
},
sx: { backgroundImage: 'none' },
},
}}
>
<DialogTitle>Reset password</DialogTitle>
<DialogContent
sx={{ display: 'flex', flexDirection: 'column', gap: 2, width: '100%' }}
>
<DialogContentText>
Enter your account's email address, and we'll send you a link to
reset your password.
</DialogContentText>
<OutlinedInput
autoFocus
required
margin="dense"
id="email"
name="email"
label="Email address"
placeholder="Email address"
type="email"
fullWidth
/>
</DialogContent>
<DialogActions sx={{ pb: 3, px: 3 }}>
<Button onClick={handleClose}>Cancel</Button>
<Button variant="contained" type="submit">
Continue
</Button>
</DialogActions>
</Dialog>
);
}
Changed for this site: Uses MUI's default theme from the setup instead of the template's AppTheme and colour-mode switch, so it needs nothing beyond @mui/material; the Google and Facebook buttons use one-colour icons from @mui/icons-material instead of the brand-coloured logos; no Sitemark logo, full-window height, page gradient or autofocus (the form sits inside a page); the link at the bottom points to #.