EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
ResetPassword.jsx
Go to the documentation of this file.
1import { useState, useEffect } from 'react';
2import { useSearchParams, Link } from 'react-router-dom';
3import PublicLayout from '../components/Layout/PublicLayout';
4import { apiFetch } from '../lib/api';
5
6export default function ResetPassword() {
7 const [params] = useSearchParams();
8 const token = params.get('token') || '';
9 const [password, setPassword] = useState('');
10 const [confirm, setConfirm] = useState('');
11 const [ok, setOk] = useState(false);
12 const [loading, setLoading] = useState(false);
13 const [error, setError] = useState('');
14
15 async function handleSubmit(e) {
16 e.preventDefault();
17 if (password !== confirm) { setError('Passwords do not match'); return; }
18 setLoading(true);
19 setError('');
20 try {
21 const res = await fetch('/users/reset-password', {
22 method: 'POST',
23 headers: { 'Content-Type': 'application/json' },
24 body: JSON.stringify({ token, password })
25 });
26 const ct = res.headers.get('content-type') || '';
27 const data = ct.includes('json') ? await res.json() : { };
28 if (!res.ok) throw new Error(data.error || 'Reset failed');
29 setOk(true);
30 } catch (err) {
31 setError(err.message || 'Error');
32 } finally {
33 setLoading(false);
34 }
35 }
36
37 return (
38 <PublicLayout>
39 <div style={{ maxWidth: 480, margin: '3rem auto', padding: '2rem', background: '#fff', borderRadius: 12, boxShadow: '0 8px 24px rgba(0,0,0,0.08)' }}>
40 <h1>Reset Password</h1>
41 {ok ? (
42 <div>
43 <p>Your password has been reset. You can now <Link to="/login">log in</Link>.</p>
44 </div>
45 ) : (
46 <form onSubmit={handleSubmit}>
47 <div style={{ marginBottom: 12 }}>
48 <label>New Password</label>
49 <input type="password" value={password} onChange={(e)=>setPassword(e.target.value)} required style={{ width: '100%', padding: 10, marginTop: 6 }} />
50 </div>
51 <div style={{ marginBottom: 12 }}>
52 <label>Confirm Password</label>
53 <input type="password" value={confirm} onChange={(e)=>setConfirm(e.target.value)} required style={{ width: '100%', padding: 10, marginTop: 6 }} />
54 </div>
55 {error && <div style={{ color: 'red', marginBottom: 8 }}>{error}</div>}
56 <button disabled={loading} style={{ padding: '10px 16px' }}>{loading ? 'Resetting…' : 'Reset Password'}</button>
57 </form>
58 )}
59 </div>
60 </PublicLayout>
61 );
62}