+// noinspection JSValidateTypes
+
/**
* @file Login view with an authentication form
*/
import React, { useContext } from 'react';
-import { Container, TextField, Button, Typography, Box } from '@mui/material';
-import { useForm, Controller } from 'react-hook-form';
+import { Box, Button, Container, TextField, Typography } from '@mui/material';
+import { Controller, useForm } from 'react-hook-form';
import { yupResolver } from '@hookform/resolvers/yup';
import * as yup from 'yup';
import { useSnackbar } from 'notistack';
import { AuthContext } from '../../components/AuthContext';
-const schema = yup.object({
- email: yup.string().email('Invalid email').required('Email is required'),
- password: yup.string().required('Password is required'),
-}).required();
+const schema = yup.object( {
+ email:yup.string().email( 'Invalid email' ).required( 'Email is required' ),
+ password:yup.string().required( 'Password is required' ),
+} ).required();
const Login = () => {
const { enqueueSnackbar } = useSnackbar();
- const { login } = useContext(AuthContext) || {};
- const { control, handleSubmit, formState: { errors } } = useForm({
- resolver: yupResolver(schema),
- });
+ const { login } = useContext( AuthContext ) || {};
+ const { control, handleSubmit, formState:{ errors } } = useForm( {
+ resolver:yupResolver( schema ),
+ } );
- const onSubmit = async (data) => {
- const success = await login(data.email, data.password);
+ const onSubmit = async ( data ) => {
+ const success = await login( data.email, data.password );
if (success) {
- enqueueSnackbar('Login successful', { variant: 'success' });
+ enqueueSnackbar( 'Login successful', { variant:'success' } );
} else {
- enqueueSnackbar('Invalid credentials', { variant: 'error' });
+ enqueueSnackbar( 'Invalid credentials', { variant:'error' } );
}
};
return (
- <div>
- <Container maxWidth="sm" className="container">
- <Typography variant="h4" className="text-3xl font-bold mb-6 mt-4">
+ <div className="flex items-center justify-center min-h-screen bg-gray-100">
+ <Container maxWidth="sm"
+ className="container">
+ <Typography variant="h4"
+ className="text-3xl font-bold mb-6 mt-4">
Login to PHS Admin
</Typography>
- <Box component="form" onSubmit={handleSubmit(onSubmit)} className="space-y-4">
- <Controller
- name="email"
- control={control}
- defaultValue=""
- render={({ field }) => (
- <TextField
- {...field}
- label="Email"
- fullWidth
- error={!!errors.email}
- helperText={errors.email?.message}
- className="mb-4"
- />
- )}
- />
- <Controller
- name="password"
- control={control}
- defaultValue=""
- render={({ field }) => (
- <TextField
- {...field}
- label="Password"
- type="password"
- fullWidth
- error={!!errors.password}
- helperText={errors.password?.message}
- className="mb-4"
- />
- )}
- />
- <Button type="submit" variant="contained" color="primary" fullWidth>
+ <Box component="form"
+ onSubmit={ handleSubmit( onSubmit ) }
+ className="space-y-4">
+ <Controller name="email"
+ control={ control }
+ defaultValue=""
+ render={ ( { field } ) => (
+ <TextField
+ { ...field } label="Email"
+ fullWidth
+ error={ !!errors.email }
+ helperText={ errors.email?.message }
+ className="mb-4"/>
+ ) }/>
+ <Controller name="password"
+ control={ control }
+ defaultValue=""
+ render={ ( { field } ) => (
+ <TextField
+ { ...field } label="Password"
+ type="password"
+ fullWidth
+ error={ !!errors.password }
+ helperText={ errors.password?.message }
+ className="mb-4"/>
+ ) }/>
+ <Button type="submit"
+ variant="contained"
+ color="primary"
+ fullWidth>
Login
</Button>
</Box>