Add reset password button to admin user page
This commit is contained in:
@@ -4,6 +4,7 @@ import { prisma } from "@/lib/prisma";
|
||||
import { headers } from "next/headers";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import bcrypt from "bcryptjs";
|
||||
|
||||
async function requireAdmin() {
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
@@ -78,6 +79,26 @@ export async function deleteBalanceTransaction(userId: string, txId: string) {
|
||||
revalidatePath(`/admin/users/${userId}`);
|
||||
}
|
||||
|
||||
export async function resetUserPassword(userId: string): Promise<{ tempPassword: string }> {
|
||||
await requireAdmin();
|
||||
|
||||
const account = await prisma.account.findFirst({
|
||||
where: { userId, providerId: "credential" },
|
||||
});
|
||||
if (!account) throw new Error("Аккаунт с паролем не найден");
|
||||
|
||||
const chars = "ABCDEFGHJKMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789";
|
||||
const tempPassword = Array.from({ length: 10 }, () => chars[Math.floor(Math.random() * chars.length)]).join("");
|
||||
|
||||
const hash = await bcrypt.hash(tempPassword, 10);
|
||||
await prisma.account.update({
|
||||
where: { id: account.id },
|
||||
data: { password: hash },
|
||||
});
|
||||
|
||||
return { tempPassword };
|
||||
}
|
||||
|
||||
export async function revokeUserAccess(userId: string, courseId: string) {
|
||||
const session = await requireAdmin();
|
||||
await prisma.courseEnrollment.delete({
|
||||
|
||||
@@ -4,6 +4,7 @@ import Link from "next/link";
|
||||
import { UserEnrollmentManager } from "@/components/admin/user-enrollment-manager";
|
||||
import { UserContactEditor } from "@/components/admin/user-contact-editor";
|
||||
import { UserBalanceBlock } from "@/components/admin/user-balance-block";
|
||||
import { ResetPasswordButton } from "@/components/admin/reset-password-button";
|
||||
|
||||
interface Props {
|
||||
params: Promise<{ userId: string }>;
|
||||
@@ -77,6 +78,14 @@ export default async function UserPage({ params }: Props) {
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{/* Reset password */}
|
||||
<section className="card-aubade p-6 mb-6">
|
||||
<p className="text-xs font-bold uppercase tracking-widest mb-4" style={{ color: "var(--muted-foreground)" }}>
|
||||
Пароль
|
||||
</p>
|
||||
<ResetPasswordButton userId={userId} />
|
||||
</section>
|
||||
|
||||
{/* Balance */}
|
||||
<section className="card-aubade p-6 mb-6">
|
||||
<p className="text-xs font-bold uppercase tracking-widest mb-5" style={{ color: "var(--muted-foreground)" }}>
|
||||
|
||||
Reference in New Issue
Block a user