Add reset password button to admin user page
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { resetUserPassword } from "@/app/admin/users/[userId]/actions";
|
||||
|
||||
export function ResetPasswordButton({ userId }: { userId: string }) {
|
||||
const [tempPassword, setTempPassword] = useState<string | null>(null);
|
||||
const [isPending, setIsPending] = useState(false);
|
||||
const [copied, setCopied] = useState(false);
|
||||
|
||||
async function handleReset() {
|
||||
if (!confirm("Сгенерировать новый временный пароль для пользователя?")) return;
|
||||
setIsPending(true);
|
||||
setTempPassword(null);
|
||||
try {
|
||||
const { tempPassword: pw } = await resetUserPassword(userId);
|
||||
setTempPassword(pw);
|
||||
} catch (e) {
|
||||
alert(e instanceof Error ? e.message : "Ошибка");
|
||||
} finally {
|
||||
setIsPending(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleCopy() {
|
||||
if (!tempPassword) return;
|
||||
await navigator.clipboard.writeText(tempPassword);
|
||||
setCopied(true);
|
||||
setTimeout(() => setCopied(false), 2000);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
<button
|
||||
onClick={handleReset}
|
||||
disabled={isPending}
|
||||
className="btn-aubade text-sm"
|
||||
style={isPending ? { opacity: 0.6, cursor: "not-allowed" } : undefined}
|
||||
>
|
||||
{isPending ? "Генерация..." : "Сбросить пароль"}
|
||||
</button>
|
||||
|
||||
{tempPassword && (
|
||||
<div className="p-3 text-sm space-y-2" style={{ border: "2px solid var(--border)", backgroundColor: "var(--color-highlight)" }}>
|
||||
<p className="text-xs font-bold uppercase tracking-widest" style={{ color: "var(--muted-foreground)" }}>
|
||||
Временный пароль — передай пользователю
|
||||
</p>
|
||||
<div className="flex items-center gap-3">
|
||||
<code className="text-base font-bold tracking-wider">{tempPassword}</code>
|
||||
<button
|
||||
onClick={handleCopy}
|
||||
className="text-xs underline"
|
||||
style={{ color: "var(--muted-foreground)" }}
|
||||
>
|
||||
{copied ? "Скопировано" : "Копировать"}
|
||||
</button>
|
||||
</div>
|
||||
<p className="text-xs" style={{ color: "var(--muted-foreground)" }}>
|
||||
Пользователь сможет сменить пароль в профиле после входа.
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user