source: src/sections/auth/firebase/firebase-login-view.tsx@ 5d6f37a

main
Last change on this file since 5d6f37a was 5d6f37a, checked in by Naum Shapkarovski <naumshapkarovski@…>, 7 weeks ago

add customer

  • Property mode set to 100644
File size: 3.1 KB
Line 
1'use client';
2
3import * as Yup from 'yup';
4import { useForm } from 'react-hook-form';
5import { useState } from 'react';
6import { yupResolver } from '@hookform/resolvers/yup';
7// @mui
8import LoadingButton from '@mui/lab/LoadingButton';
9import Alert from '@mui/material/Alert';
10import Stack from '@mui/material/Stack';
11import IconButton from '@mui/material/IconButton';
12import Typography from '@mui/material/Typography';
13import InputAdornment from '@mui/material/InputAdornment';
14// routes
15import { useSearchParams, useRouter } from 'src/routes/hooks';
16// config
17import { PATH_AFTER_LOGIN } from 'src/config-global';
18// hooks
19import { useBoolean } from 'src/hooks/use-boolean';
20// auth
21import { useAuthContext } from 'src/auth/hooks';
22// components
23import Iconify from 'src/components/iconify';
24import FormProvider, { RHFTextField } from 'src/components/hook-form';
25
26// ----------------------------------------------------------------------
27
28export 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 }}>
76 <Typography variant="h4">Sign in to MVP Masters</Typography>
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}
Note: See TracBrowser for help on using the repository browser.