changement
This commit is contained in:
139
frontend/src/pages/NouveauMotDePasse.jsx
Normal file
139
frontend/src/pages/NouveauMotDePasse.jsx
Normal file
@@ -0,0 +1,139 @@
|
||||
import { useState } from 'react'
|
||||
import { Navigate, useNavigate } from 'react-router-dom'
|
||||
import { setInitialPasswordApi } from '../services/auth'
|
||||
import { useAuth } from '../context/AuthContext'
|
||||
import { homeFor } from '../config/roles'
|
||||
import PasswordRules from '../components/PasswordRules'
|
||||
import { isPasswordValid, PASSWORD_RULES_ERROR } from '../utils/passwordRules'
|
||||
|
||||
// Première connexion d'un employé créé par un admin : le mot de passe reçu
|
||||
// par email est provisoire, il doit être remplacé avant tout accès.
|
||||
export default function NouveauMotDePasse() {
|
||||
const navigate = useNavigate()
|
||||
const { user, login, logout } = useAuth()
|
||||
|
||||
const [form, setForm] = useState({ password: '', confirmPassword: '' })
|
||||
const [showPassword, setShowPassword] = useState(false)
|
||||
const [error, setError] = useState('')
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
||||
if (!user) return <Navigate to="/connexion" replace />
|
||||
if (!user.mustChangePassword) return <Navigate to={homeFor(user)} replace />
|
||||
|
||||
const handleChange = (e) => setForm({ ...form, [e.target.name]: e.target.value })
|
||||
|
||||
const handleSubmit = async (e) => {
|
||||
e.preventDefault()
|
||||
setError('')
|
||||
|
||||
if (!isPasswordValid(form.password)) {
|
||||
setError(PASSWORD_RULES_ERROR)
|
||||
return
|
||||
}
|
||||
if (form.password !== form.confirmPassword) {
|
||||
setError('Les deux mots de passe ne correspondent pas.')
|
||||
return
|
||||
}
|
||||
|
||||
setLoading(true)
|
||||
try {
|
||||
await setInitialPasswordApi(form)
|
||||
const updatedUser = { ...user, mustChangePassword: false }
|
||||
login(updatedUser, localStorage.getItem('token'))
|
||||
navigate(homeFor(updatedUser), { replace: true })
|
||||
} catch (err) {
|
||||
if (err.status === 401) {
|
||||
logout()
|
||||
navigate('/connexion', { replace: true })
|
||||
return
|
||||
}
|
||||
setError(err.message)
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleLogout = () => {
|
||||
logout()
|
||||
navigate('/connexion', { replace: true })
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-white flex flex-col justify-center px-8 py-12 max-w-sm mx-auto">
|
||||
<div className="mb-8">
|
||||
<h1 className="text-4xl font-bold italic font-serif text-[#006B6B]">Nouveau mot de passe</h1>
|
||||
<p className="text-gray-500 mt-2 text-sm">
|
||||
Bienvenue {user.firstname} ! Vous vous êtes connecté avec un mot de passe provisoire.
|
||||
Choisissez votre propre mot de passe pour accéder à votre espace.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="mb-4 px-4 py-3 rounded-lg bg-red-50 border border-red-200 text-red-600 text-sm" role="alert">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<form onSubmit={handleSubmit} className="space-y-5">
|
||||
<div>
|
||||
<label htmlFor="new-password" className="block text-sm font-medium text-gray-800 mb-1">
|
||||
Nouveau mot de passe <span className="text-gray-800">*</span>
|
||||
</label>
|
||||
<input
|
||||
type={showPassword ? 'text' : 'password'}
|
||||
id="new-password"
|
||||
name="password"
|
||||
value={form.password}
|
||||
onChange={handleChange}
|
||||
required
|
||||
autoComplete="new-password"
|
||||
aria-describedby="new-password-rules"
|
||||
placeholder="Votre nouveau mot de passe"
|
||||
className="w-full border border-gray-200 rounded-lg px-4 py-3 text-sm text-gray-700 placeholder-gray-500 focus:outline-none focus:ring-1 focus:ring-[#006B6B] focus:border-[#006B6B]"
|
||||
/>
|
||||
<PasswordRules id="new-password-rules" password={form.password} />
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label htmlFor="confirm-new-password" className="block text-sm font-medium text-gray-800 mb-1">
|
||||
Confirmer le mot de passe <span className="text-gray-800">*</span>
|
||||
</label>
|
||||
<input
|
||||
type={showPassword ? 'text' : 'password'}
|
||||
id="confirm-new-password"
|
||||
name="confirmPassword"
|
||||
value={form.confirmPassword}
|
||||
onChange={handleChange}
|
||||
required
|
||||
autoComplete="new-password"
|
||||
placeholder="Saisissez-le à nouveau"
|
||||
className="w-full border border-gray-200 rounded-lg px-4 py-3 text-sm text-gray-700 placeholder-gray-500 focus:outline-none focus:ring-1 focus:ring-[#006B6B] focus:border-[#006B6B]"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<label className="flex items-center gap-2 cursor-pointer">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={showPassword}
|
||||
onChange={(e) => setShowPassword(e.target.checked)}
|
||||
className="w-5 h-5 accent-[#006B6B]"
|
||||
/>
|
||||
<span className="text-xs text-gray-500">Afficher les mots de passe</span>
|
||||
</label>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
className="w-full bg-[#006B6B] hover:bg-[#005959] disabled:opacity-60 text-white font-medium py-3 rounded-lg transition-colors"
|
||||
>
|
||||
{loading ? 'Enregistrement…' : 'Enregistrer mon mot de passe'}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<p className="text-center text-sm text-gray-500 mt-8">
|
||||
<button type="button" onClick={handleLogout} className="text-gray-700 font-medium hover:underline">
|
||||
Se déconnecter
|
||||
</button>
|
||||
</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user