forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordResetCard.tsx
More file actions
214 lines (190 loc) · 7.75 KB
/
Copy pathPasswordResetCard.tsx
File metadata and controls
214 lines (190 loc) · 7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"use client";
import Link from "next/link";
import { useEffect, useState } from "react";
import type {
AuthErrorResponse,
PasswordResetResponse,
PasswordResetStatus,
} from "../lib/auth";
type PasswordResetCardProps = {
token: string;
};
export default function PasswordResetCard({ token }: PasswordResetCardProps) {
const [password, setPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const [pending, setPending] = useState(false);
const [checking, setChecking] = useState(true);
const [valid, setValid] = useState(false);
const [error, setError] = useState<string | null>(null);
const [message, setMessage] = useState<string | null>(null);
useEffect(() => {
let cancelled = false;
const validateToken = async () => {
try {
const response = await fetch(
`/api/auth/password-reset/${encodeURIComponent(token)}`,
{ cache: "no-store" },
);
const result = (await response.json()) as
| PasswordResetStatus
| AuthErrorResponse;
if (!response.ok) {
throw new Error(
"error" in result && result.error
? result.error
: "No se pudo validar el link",
);
}
if (!cancelled) {
const isValid = "valid" in result && Boolean(result.valid);
setValid(isValid);
if (!isValid) {
setError(
"El link de recuperacion es invalido o ya vencio.",
);
}
}
} catch (validationError) {
if (!cancelled) {
setError(
validationError instanceof Error
? validationError.message
: "Ocurrio un error inesperado",
);
}
} finally {
if (!cancelled) {
setChecking(false);
}
}
};
void validateToken();
return () => {
cancelled = true;
};
}, [token]);
const submit = async (
event: React.SyntheticEvent<HTMLFormElement, SubmitEvent>,
) => {
event.preventDefault();
if (password !== confirmPassword) {
setError("Las passwords no coinciden");
return;
}
setPending(true);
setError(null);
setMessage(null);
try {
const response = await fetch("/api/auth/password-reset/confirm", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ token, password }),
});
const result = (await response.json()) as
| PasswordResetResponse
| AuthErrorResponse;
if (!response.ok) {
throw new Error(
"error" in result && result.error
? result.error
: "No se pudo actualizar la password",
);
}
setMessage(
"message" in result
? result.message
: "La password se actualizo correctamente.",
);
setValid(false);
setPassword("");
setConfirmPassword("");
} catch (submitError) {
setError(
submitError instanceof Error
? submitError.message
: "Ocurrio un error inesperado",
);
} finally {
setPending(false);
}
};
return (
<div className="mx-auto w-full max-w-md overflow-hidden rounded-[32px] border border-stone-700/70 bg-stone-950/88 text-stone-100 shadow-2xl backdrop-blur-md">
<div className="border-b border-white/8 bg-[radial-gradient(circle_at_top,#f59e0b33,transparent_55%),linear-gradient(135deg,#111827,#0f172a)] px-6 py-6">
<p className="text-[11px] uppercase tracking-[0.34em] text-amber-200/80">
AOWeb
</p>
<h1 className="mt-2 text-3xl font-semibold text-stone-50">
Elegi una nueva contraseña
</h1>
<p className="mt-2 text-sm text-stone-300/80">
Usa una clave de al menos 8 caracteres. El link sirve una
sola vez.
</p>
</div>
<div className="p-6">
{checking ? (
<div className="rounded-2xl bg-white/5 px-4 py-3 text-sm text-stone-300">
Validando link seguro...
</div>
) : null}
{!checking && valid ? (
<form className="space-y-3" onSubmit={submit}>
<input
value={password}
onChange={(event) =>
setPassword(event.target.value)
}
className="w-full rounded-2xl border border-stone-700 bg-stone-900/90 px-4 py-3 text-sm outline-none transition focus:border-amber-400"
placeholder="Nueva contraseña"
type="password"
autoComplete="new-password"
minLength={8}
required
/>
<input
value={confirmPassword}
onChange={(event) =>
setConfirmPassword(event.target.value)
}
className="w-full rounded-2xl border border-stone-700 bg-stone-900/90 px-4 py-3 text-sm outline-none transition focus:border-amber-400"
placeholder="Confirmar nueva contraseña"
type="password"
autoComplete="new-password"
minLength={8}
required
/>
<button
type="submit"
disabled={pending}
className="w-full rounded-2xl bg-amber-300 px-4 py-3 text-sm font-semibold text-stone-950 transition hover:bg-amber-200 disabled:cursor-not-allowed disabled:bg-stone-700 disabled:text-stone-400"
>
{pending ? "Guardando..." : "Actualizar contraseña"}
</button>
</form>
) : null}
{message ? (
<div className="mt-4 rounded-2xl bg-emerald-500/12 px-4 py-3 text-sm text-emerald-200">
{message}
</div>
) : null}
{error ? (
<div className="mt-4 rounded-2xl bg-rose-500/12 px-4 py-3 text-sm text-rose-200">
{error}
</div>
) : null}
<div className="mt-5 border-t border-white/8 pt-4 text-sm text-stone-400">
<Link
href="/login"
prefetch={false}
className="font-medium text-cyan-300 transition hover:text-cyan-200"
>
Ir a inicio de sesión
</Link>
</div>
</div>
</div>
);
}