[5d6f37a] | 1 | 'use client';
|
---|
| 2 |
|
---|
| 3 | import * as Yup from 'yup';
|
---|
| 4 | import { useForm } from 'react-hook-form';
|
---|
| 5 | import { useState } from 'react';
|
---|
| 6 | import { yupResolver } from '@hookform/resolvers/yup';
|
---|
| 7 | // @mui
|
---|
| 8 | import LoadingButton from '@mui/lab/LoadingButton';
|
---|
| 9 | import Alert from '@mui/material/Alert';
|
---|
| 10 | import Stack from '@mui/material/Stack';
|
---|
| 11 | import IconButton from '@mui/material/IconButton';
|
---|
| 12 | import Typography from '@mui/material/Typography';
|
---|
| 13 | import InputAdornment from '@mui/material/InputAdornment';
|
---|
| 14 | // routes
|
---|
| 15 | import { useSearchParams, useRouter } from 'src/routes/hooks';
|
---|
| 16 | // config
|
---|
| 17 | import { PATH_AFTER_LOGIN } from 'src/config-global';
|
---|
| 18 | // hooks
|
---|
| 19 | import { useBoolean } from 'src/hooks/use-boolean';
|
---|
| 20 | // auth
|
---|
| 21 | import { useAuthContext } from 'src/auth/hooks';
|
---|
| 22 | // components
|
---|
| 23 | import Iconify from 'src/components/iconify';
|
---|
| 24 | import FormProvider, { RHFTextField } from 'src/components/hook-form';
|
---|
| 25 |
|
---|
| 26 | // ----------------------------------------------------------------------
|
---|
| 27 |
|
---|
| 28 | export default function FirebaseLoginView() {
|
---|
| 29 | const { login } = useAuthContext();
|
---|
| 30 |
|
---|
| 31 | const router = useRouter();
|
---|
| 32 |
|
---|
| 33 | const [errorMsg, setErrorMsg] = useState('');
|
---|
| 34 |
|
---|
| 35 | const searchParams = useSearchParams();
|
---|
| 36 |
|
---|
| 37 | const returnTo = searchParams.get('returnTo');
|
---|
| 38 |
|
---|
| 39 | const password = useBoolean();
|
---|
| 40 |
|
---|
| 41 | const LoginSchema = Yup.object().shape({
|
---|
| 42 | email: Yup.string().required('Email is required').email('Email must be a valid email address'),
|
---|
| 43 | password: Yup.string().required('Password is required'),
|
---|
| 44 | });
|
---|
| 45 |
|
---|
| 46 | const defaultValues = {
|
---|
| 47 | email: '',
|
---|
| 48 | password: '',
|
---|
| 49 | };
|
---|
| 50 |
|
---|
| 51 | const methods = useForm({
|
---|
| 52 | resolver: yupResolver(LoginSchema),
|
---|
| 53 | defaultValues,
|
---|
| 54 | });
|
---|
| 55 |
|
---|
| 56 | const {
|
---|
| 57 | reset,
|
---|
| 58 | handleSubmit,
|
---|
| 59 | formState: { isSubmitting },
|
---|
| 60 | } = methods;
|
---|
| 61 |
|
---|
| 62 | const onSubmit = handleSubmit(async (data) => {
|
---|
| 63 | try {
|
---|
| 64 | await login?.(data.email, data.password);
|
---|
| 65 |
|
---|
| 66 | router.push(returnTo || PATH_AFTER_LOGIN);
|
---|
| 67 | } catch (error) {
|
---|
| 68 | console.error(error);
|
---|
| 69 | reset();
|
---|
| 70 | setErrorMsg(typeof error === 'string' ? error : error.message);
|
---|
| 71 | }
|
---|
| 72 | });
|
---|
| 73 |
|
---|
| 74 | const renderHead = (
|
---|
| 75 | <Stack spacing={2} sx={{ mb: 5 }}>
|
---|
[299af01] | 76 | <Typography variant="h4">Sign in to AgencyOS</Typography>
|
---|
[5d6f37a] | 77 | </Stack>
|
---|
| 78 | );
|
---|
| 79 |
|
---|
| 80 | const renderForm = (
|
---|
| 81 | <Stack spacing={2.5}>
|
---|
| 82 | {!!errorMsg && <Alert severity="error">{errorMsg}</Alert>}
|
---|
| 83 |
|
---|
| 84 | <RHFTextField name="email" label="Email address" />
|
---|
| 85 |
|
---|
| 86 | <RHFTextField
|
---|
| 87 | name="password"
|
---|
| 88 | label="Password"
|
---|
| 89 | type={password.value ? 'text' : 'password'}
|
---|
| 90 | InputProps={{
|
---|
| 91 | endAdornment: (
|
---|
| 92 | <InputAdornment position="end">
|
---|
| 93 | <IconButton onClick={password.onToggle} edge="end">
|
---|
| 94 | <Iconify icon={password.value ? 'solar:eye-bold' : 'solar:eye-closed-bold'} />
|
---|
| 95 | </IconButton>
|
---|
| 96 | </InputAdornment>
|
---|
| 97 | ),
|
---|
| 98 | }}
|
---|
| 99 | />
|
---|
| 100 |
|
---|
| 101 | <LoadingButton
|
---|
| 102 | fullWidth
|
---|
| 103 | color="inherit"
|
---|
| 104 | size="large"
|
---|
| 105 | type="submit"
|
---|
| 106 | variant="contained"
|
---|
| 107 | loading={isSubmitting}
|
---|
| 108 | >
|
---|
| 109 | Login
|
---|
| 110 | </LoadingButton>
|
---|
| 111 | </Stack>
|
---|
| 112 | );
|
---|
| 113 |
|
---|
| 114 | return (
|
---|
| 115 | <FormProvider methods={methods} onSubmit={onSubmit}>
|
---|
| 116 | {renderHead}
|
---|
| 117 |
|
---|
| 118 | {renderForm}
|
---|
| 119 | </FormProvider>
|
---|
| 120 | );
|
---|
| 121 | }
|
---|