2022-02-07 16:24:42 +04:00
|
|
|
|
import {useState, forwardRef} from 'react'
|
|
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
|
|
import axios from 'axios'
|
2023-07-22 13:36:33 +04:00
|
|
|
|
import {useRouter} from 'next/navigation'
|
2022-02-07 16:24:42 +04:00
|
|
|
|
|
|
|
|
|
|
import {FormControl, Snackbar, IconButton, Button, Input, InputAdornment, InputLabel, Box, Container, Typography, LinearProgress} from '@mui/material'
|
|
|
|
|
|
import MuiAlert from '@mui/material/Alert'
|
|
|
|
|
|
import Visibility from '@mui/icons-material/Visibility'
|
|
|
|
|
|
import VisibilityOff from '@mui/icons-material/VisibilityOff'
|
|
|
|
|
|
import Link from '@mui/material/Link'
|
|
|
|
|
|
|
|
|
|
|
|
const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:1337'
|
|
|
|
|
|
|
|
|
|
|
|
const Alert = forwardRef(function Alert(props, ref) {
|
|
|
|
|
|
return <MuiAlert ref={ref} elevation={6} variant='filled' {...props} />
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
export default function NewPassword({code}) {
|
|
|
|
|
|
const router = useRouter()
|
|
|
|
|
|
const [showPassword, setShowPassword] = useState('')
|
|
|
|
|
|
const [showPasswordConfirmation, setShowPasswordConfirmation] = useState('')
|
|
|
|
|
|
const [password, setPassword] = useState('')
|
|
|
|
|
|
const [passwordConfirmation, setPasswordConfirmation] = useState('')
|
|
|
|
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
|
|
const [error, setError] = useState('')
|
|
|
|
|
|
const [open, setOpen] = useState(true)
|
|
|
|
|
|
const [passwordSuccess, setPasswordSuccess] = useState(false)
|
|
|
|
|
|
|
|
|
|
|
|
const resetForm = () => {
|
|
|
|
|
|
setPassword('')
|
|
|
|
|
|
setPasswordConfirmation('')
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const changePassword = async () => {
|
|
|
|
|
|
setLoading(true)
|
|
|
|
|
|
try {
|
|
|
|
|
|
await axios.post(`${API_URL}/auth/reset-password`, {
|
|
|
|
|
|
code,
|
|
|
|
|
|
password,
|
|
|
|
|
|
passwordConfirmation
|
|
|
|
|
|
})
|
|
|
|
|
|
setLoading(false)
|
|
|
|
|
|
setPasswordSuccess(true)
|
|
|
|
|
|
setTimeout(() => {
|
2023-07-22 13:43:05 +04:00
|
|
|
|
router.push('/pwopose')
|
2022-02-07 16:24:42 +04:00
|
|
|
|
}, 10_000)
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
setOpen(true)
|
|
|
|
|
|
setError('Une erreur s’est produite. Veuillez réessayer ultérieurement')
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const handleMouseDownPassword = event => {
|
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const handleMouseDownPasswordConfirmation = event => {
|
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const handleKeyUpPassword = event => {
|
|
|
|
|
|
if (event.keyCode === 13) {
|
|
|
|
|
|
handleClick()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const handleKeyUpPasswordConfirmation = event => {
|
|
|
|
|
|
if (event.keyCode === 13) {
|
|
|
|
|
|
handleClick()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const handleClick = async () => {
|
|
|
|
|
|
if (password !== passwordConfirmation) {
|
|
|
|
|
|
setOpen(true)
|
|
|
|
|
|
setError('Les 2 mots de passe de correspondent pas')
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (password.length < 6) {
|
|
|
|
|
|
setOpen(true)
|
|
|
|
|
|
setError('Le mot de passe est trop court, 6 caratères minimum')
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await changePassword()
|
|
|
|
|
|
setLoading(false)
|
|
|
|
|
|
resetForm()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const handleClose = (event, reason) => {
|
|
|
|
|
|
if (reason === 'clickaway') {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setOpen(false)
|
|
|
|
|
|
setError('')
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Container sx={{marginTop: 2}} maxWidth='sm'>
|
|
|
|
|
|
<Box sx={{width: '100%'}}>
|
|
|
|
|
|
<Typography sx={{textAlign: 'center'}} variant='h5' component='div'>Nouveau mot de passe</Typography>
|
|
|
|
|
|
<FormControl fullWidth style={{marginTop: '1em'}}>
|
|
|
|
|
|
<InputLabel htmlFor='password'>Mot de passe</InputLabel>
|
|
|
|
|
|
<Input
|
|
|
|
|
|
value={password}
|
|
|
|
|
|
name='register-password'
|
|
|
|
|
|
type={showPassword ? 'text' : 'password'}
|
|
|
|
|
|
id='register-password'
|
|
|
|
|
|
endAdornment={
|
|
|
|
|
|
<InputAdornment position='end'>
|
|
|
|
|
|
<IconButton
|
|
|
|
|
|
aria-label='password visibility'
|
|
|
|
|
|
size='large'
|
|
|
|
|
|
onClick={() => setShowPassword(!showPassword)}
|
|
|
|
|
|
onMouseDown={handleMouseDownPassword}
|
|
|
|
|
|
>
|
|
|
|
|
|
{showPassword ? <Visibility /> : <VisibilityOff />}
|
|
|
|
|
|
</IconButton>
|
|
|
|
|
|
</InputAdornment>
|
|
|
|
|
|
}
|
|
|
|
|
|
onChange={event => setPassword(event.target.value)}
|
|
|
|
|
|
onKeyUp={handleKeyUpPassword}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</FormControl>
|
|
|
|
|
|
<FormControl fullWidth style={{marginTop: '1em'}}>
|
|
|
|
|
|
<InputLabel htmlFor='password'>Vérification du mot de passe</InputLabel>
|
|
|
|
|
|
<Input
|
|
|
|
|
|
value={passwordConfirmation}
|
|
|
|
|
|
name='verification-password'
|
|
|
|
|
|
type={showPasswordConfirmation ? 'text' : 'password'}
|
|
|
|
|
|
id='verification-password'
|
|
|
|
|
|
error={password !== passwordConfirmation}
|
|
|
|
|
|
endAdornment={
|
|
|
|
|
|
<InputAdornment position='end'>
|
|
|
|
|
|
<IconButton
|
|
|
|
|
|
aria-label='password visibility'
|
|
|
|
|
|
size='large'
|
|
|
|
|
|
onClick={() => setShowPasswordConfirmation(!showPassword)}
|
|
|
|
|
|
onMouseDown={handleMouseDownPasswordConfirmation}
|
|
|
|
|
|
>
|
|
|
|
|
|
{showPasswordConfirmation ? <Visibility /> : <VisibilityOff />}
|
|
|
|
|
|
</IconButton>
|
|
|
|
|
|
</InputAdornment>
|
|
|
|
|
|
}
|
|
|
|
|
|
onChange={event => setPasswordConfirmation(event.target.value)}
|
|
|
|
|
|
onKeyUp={handleKeyUpPasswordConfirmation}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</FormControl>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
fullWidth
|
|
|
|
|
|
style={{marginTop: 20}}
|
|
|
|
|
|
variant='contained'
|
|
|
|
|
|
color='primary'
|
|
|
|
|
|
disabled={loading || password !== passwordConfirmation || password === ''}
|
|
|
|
|
|
onClick={handleClick}
|
|
|
|
|
|
>
|
|
|
|
|
|
<Typography style={{fontWeight: 'bold'}}>
|
|
|
|
|
|
Changer de mot de passe
|
|
|
|
|
|
</Typography>
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
fullWidth
|
|
|
|
|
|
style={{marginTop: 20}}
|
|
|
|
|
|
variant='outlined'
|
|
|
|
|
|
color='primary'
|
2023-07-22 13:43:05 +04:00
|
|
|
|
onClick={() => router.push('/pwopose')}
|
2022-02-07 16:24:42 +04:00
|
|
|
|
>
|
|
|
|
|
|
<Typography style={{fontWeight: 'bold'}}>
|
|
|
|
|
|
Retour à la page de connexion
|
|
|
|
|
|
</Typography>
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</Box>
|
|
|
|
|
|
|
|
|
|
|
|
{loading && <LinearProgress size={24} style={{width: '100%', marginTop: '1em'}} />}
|
|
|
|
|
|
|
|
|
|
|
|
{error && (
|
|
|
|
|
|
<Snackbar
|
|
|
|
|
|
open={open}
|
|
|
|
|
|
autoHideDuration={6000}
|
|
|
|
|
|
onClose={handleClose}
|
|
|
|
|
|
>
|
|
|
|
|
|
<Alert severity='error' onClose={handleClose}><strong>{error}</strong></Alert>
|
|
|
|
|
|
</Snackbar>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
{passwordSuccess && (
|
|
|
|
|
|
<Snackbar
|
|
|
|
|
|
open={passwordSuccess}
|
|
|
|
|
|
onClose={handleClose}
|
|
|
|
|
|
>
|
2023-07-22 13:43:05 +04:00
|
|
|
|
<Alert severity='info' onClose={handleClose}>Votre changement de mot de passe a été pris en compte. Vous serez redirigé vers la page de connexion. <Link underline='hover' color='inherit' href='/pwopose'><strong>Cliquez ici si vous n’êtes pas redirigé vers la page de connexion</strong></Link></Alert>
|
2022-02-07 16:24:42 +04:00
|
|
|
|
</Snackbar>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</Container>
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NewPassword.propTypes = {
|
|
|
|
|
|
code: PropTypes.string.isRequired
|
|
|
|
|
|
}
|