From 3d9d89eea6f9b14885bc19a8c5d78f50dfc980de Mon Sep 17 00:00:00 2001 From: joseCorte-exe Date: Wed, 29 Jun 2022 10:56:47 -0300 Subject: [PATCH] :sparkles: add forgot password --- src/pages/verifyEmail/index.tsx | 151 ++++++++++++++++++++++++-------- 1 file changed, 116 insertions(+), 35 deletions(-) diff --git a/src/pages/verifyEmail/index.tsx b/src/pages/verifyEmail/index.tsx index 3024581..bce5199 100644 --- a/src/pages/verifyEmail/index.tsx +++ b/src/pages/verifyEmail/index.tsx @@ -5,7 +5,8 @@ import FormData from 'form-data'; import LoginButton from '../../components/buttons/loginButton/LoginButton'; import TextField from '@mui/material/TextField'; -import Alert from '@mui/material/Alert'; +import Snackbar from '@mui/material/Snackbar' +import MuiAlert, { AlertProps } from '@mui/material/Alert' import { VerifyEmailContainer, VerifyEmailView } from '../../styles/layouts/forgotPassword/verifyEmail'; import RenderIf from '../../utils/renderIf'; @@ -14,31 +15,51 @@ import { api } from '../../services/api'; import { GetServerSideProps } from 'next'; import { parseCookies } from 'nookies'; +const Alert = React.forwardRef(function Alert( + props, + ref +) { + return +}) + export default function VerifyEmail() { const [sent, setSent]=useState(false); const [code, setCode]=useState('') + const [codeStatus, setCodeStatus]=useState(null) - const formData = new FormData(); + const [openSnackSuccess, setOpenSnackSuccess] = useState(false); const [openSnackError, setOpenSnackError] = useState(false); + const [openSnackSuccessPassword, setOpenSnackSuccessPassword] = useState(false); + const [openSnackErrorPassword, setOpenSnackErrorPassword] = useState(false); + const [email, setEmail] = useState(); - const [values, setValues] = React.useState({ - password: '', - showPassword: false, - }); + const [token, setToken] = useState(); + const [password, setPassword] = useState(); + const [password_confirmation, setPassword_confirmation] = useState(); const router = useRouter() const rota = router.pathname - useEffect(() => { - setCode('') - setSent(false) - setCodeStatus(null) - }, [rota]) + const handleCloseSnack = ( + event?: React.SyntheticEvent | Event, + reason?: string + ) => { + if (reason === 'clickaway') { + return + } + + setOpenSnackError(false) + setOpenSnackSuccess(false) + setOpenSnackErrorPassword(false) + setOpenSnackSuccessPassword(false) + } function handleSendEmail() { - formData.append('email', email) - api.post('/auth/forgot-password', formData).then(res => { + api.post('/auth/forgot-password', { + email + }).then(res => { + setSent(true) setOpenSnackSuccess(true) }).catch(res => { setOpenSnackError(true) @@ -46,36 +67,107 @@ export default function VerifyEmail() { } function verifyConfirmationCode() { - if (code === '0000') { + api.post('/auth/reset-password', { + email, + password, + password_confirmation, + token + }).then(res => { + setSent(true) + setOpenSnackSuccessPassword(true) setTimeout(() => { - router.push('/forgotPassword') - }, 2500); - setCodeStatus(true) - } else { - setCodeStatus(false) - } + router.push('/') + }, 2000); + }).catch(res => { + setOpenSnackErrorPassword(true) + }) } + useEffect(() => { + setCode('') + setSent(false) + setCodeStatus(null) + }, [rota]) + return ( Smart Energia - Verificar Email + + + Email enviado com sucesso! + + + + + Email não enviado! + + + + + Senha alterada com sucesso! + + + + + Senha não alterada! + + + router.push('/')} />

Bem-Vindo

Estratégias Inteligentes em
Gestão de Energia

- + setEmail(value.target.value)} variant="outlined"/> - handleSendEmail()} /> + { + handleSendEmail() + }} /> + - - setCode(value.target.value)} /> + setPassword(value.target.value)}/> + setPassword_confirmation(value.target.value)}/> + + setToken(value.target.value)}/> {verifyConfirmationCode()}} /> Codigo de veerificação aceito — aguarde um instante! + Codigo de verificação invalido — tente outro! @@ -93,20 +185,9 @@ export default function VerifyEmail() { } export const getServerSideProps: GetServerSideProps = async (ctx) => { - const { ['@smartAuth-token']: token } = parseCookies(ctx) - const { ['user-name']: userName } = parseCookies(ctx) - if (!token) { - return { - redirect: { - destination: '/', - permanent: false - } - } - } return { props: { - userName } } }