Files
erp-system/app/api/customers/[id]/reset-password/route.ts
T
2026-05-20 18:58:23 +00:00

41 lines
1.7 KiB
TypeScript

// /opt/erp-system/app/api/customers/[id]/reset-password/route.ts
import { NextResponse } from 'next/server';
import prisma from '../../../../../lib/prisma';
import bcrypt from 'bcryptjs';
import { sendEmail } from '../../../../../lib/email';
import { getServerSession } from "next-auth/next";
import { authOptions } from "../../../auth/[...nextauth]/route";
export async function POST(request: Request, context: { params: Promise<{ id: string }> }) {
const session = await getServerSession(authOptions);
if (!session || (session.user as any).userType !== 'TEAM') {
return NextResponse.json({ error: 'Nicht autorisiert' }, { status: 401 });
}
try {
const params = await context.params;
const customerId = parseInt(params.id);
const customer = await prisma.customer.findUnique({ where: { id: customerId } });
if (!customer) return NextResponse.json({ error: 'Kunde nicht gefunden' }, { status: 404 });
const tempPassword = Math.random().toString(36).slice(-8);
const hash = await bcrypt.hash(tempPassword, 10);
await prisma.customer.update({
where: { id: customerId },
data: { passwordHash: hash, forcePasswordChange: true }
});
await sendEmail({
to: customer.email,
subject: "Ihr Passwort wurde zurückgesetzt",
text: `Hallo ${customer.firstName},\n\nIhr Passwort für das Kundenportal wurde durch unseren Support zurückgesetzt.\n\nIhr neues Start-Passwort lautet: ${tempPassword}\n\nBitte loggen Sie sich ein. Sie werden aufgefordert, sofort ein neues, eigenes Passwort zu vergeben.\n\nViele Grüße\nIhr Support-Team`
});
return NextResponse.json({ success: true });
} catch (error) {
return NextResponse.json({ error: 'Fehler beim Reset' }, { status: 500 });
}
}