Error boundary
This commit is contained in:
42
apps/hub/app/(auth)/passwort-reset/action.ts
Normal file
42
apps/hub/app/(auth)/passwort-reset/action.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
"use server";
|
||||
|
||||
import { prisma } from "@repo/db";
|
||||
import { sendMailByTemplate } from "../../../helper/mail";
|
||||
import bcrypt from "bcryptjs";
|
||||
|
||||
export const resetPassword = async (email: string) => {
|
||||
try {
|
||||
const user = await prisma.user.findFirst({
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return { error: "Nutzer nicht gefunden" };
|
||||
}
|
||||
|
||||
const password = Math.random().toString(36).slice(-8);
|
||||
const hashedPassword = await bcrypt.hash(password, 15);
|
||||
await prisma.user.update({
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
data: {
|
||||
password: hashedPassword,
|
||||
},
|
||||
});
|
||||
|
||||
await sendMailByTemplate(user.email, "password-change", {
|
||||
user: user,
|
||||
password: password,
|
||||
});
|
||||
return {};
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
return { error: error.message };
|
||||
} else {
|
||||
return { error: "Ein Fehler ist aufgetreten" };
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user