added email verification

This commit is contained in:
PxlLoewe
2025-05-30 01:06:28 -07:00
parent 0cebe2b97e
commit b0caf56add
20 changed files with 459 additions and 232 deletions

View File

@@ -25,13 +25,14 @@ import {
} from "@radix-ui/react-icons";
import { Button } from "../../../../../_components/ui/Button";
import { Select } from "../../../../../_components/ui/Select";
import { UserSchema } from "@repo/db/zod";
import { UserOptionalDefaults, UserOptionalDefaultsSchema, UserSchema } from "@repo/db/zod";
import { useRouter } from "next/navigation";
import { PaginatedTable, PaginatedTableRef } from "_components/PaginatedTable";
import { cn } from "../../../../../../helper/cn";
import { ChartBarBigIcon, Check, Eye, PlaneIcon, Timer, X } from "lucide-react";
import Link from "next/link";
import { ColumnDef } from "@tanstack/react-table";
import { Error } from "_components/Error";
interface ProfileFormProps {
user: User;
@@ -39,15 +40,20 @@ interface ProfileFormProps {
export const ProfileForm: React.FC<ProfileFormProps> = ({ user }: ProfileFormProps) => {
const [isLoading, setIsLoading] = useState(false);
const form = useForm<User>({
defaultValues: user,
resolver: zodResolver(UserSchema),
const form = useForm<UserOptionalDefaults>({
defaultValues: {
...user,
emailVerified: user.emailVerified ?? undefined,
},
resolver: zodResolver(UserOptionalDefaultsSchema),
});
if (!user) return <Error title="User not found" statusCode={404} />;
return (
<form
className="card-body"
onSubmit={form.handleSubmit(async (values) => {
setIsLoading(true);
if (!values.id) return;
await editUser(values.id, values);
form.reset(values);
setIsLoading(false);
@@ -108,6 +114,11 @@ export const ProfileForm: React.FC<ProfileFormProps> = ({ user }: ProfileFormPro
{form.formState.errors.email && (
<p className="text-error">{form.formState.errors.email?.message}</p>
)}
<label className="label">
<input type="checkbox" {...form.register("emailVerified")} className="checkbox" />
Email bestätigt
</label>
<Select
isMulti
form={form}

View File

@@ -15,9 +15,7 @@ export const editUser = async (id: string, data: Prisma.UserUpdateInput) => {
export const resetPassword = async (id: string) => {
const array = new Uint8Array(8);
crypto.getRandomValues(array);
const password = Array.from(array, (byte) =>
("0" + (byte % 36).toString(36)).slice(-1),
).join("");
const password = Array.from(array, (byte) => ("0" + (byte % 36).toString(36)).slice(-1)).join("");
const hashedPassword = await bcrypt.hash(password, 12);
const user = await prisma.user.update({
@@ -52,3 +50,57 @@ export const deletePilotHistory = async (id: number) => {
},
});
};
export const CheckEmailCode = async (userId: string, code: string) => {
const user = await prisma.user.findUnique({
where: {
id: userId,
},
select: {
emailVerificationToken: true,
emailVerificationExpiresAt: true,
},
});
if (!user) {
return { error: "Nutzer nicht gefunden" };
}
if (user.emailVerificationToken !== code || !user.emailVerificationExpiresAt) {
return { error: "Falscher Code" };
}
if (user.emailVerificationExpiresAt < new Date()) {
return { error: "Code ist nicht mehr gültig" };
}
await prisma.user.update({
where: {
id: userId,
},
data: {
emailVerified: true,
emailVerificationToken: null,
emailVerificationExpiresAt: null,
},
});
return {
message: "Email bestätigt!",
};
};
export const sendVerificationLink = async (userId: string) => {
const code = Math.floor(10000 + Math.random() * 90000).toString();
const user = await prisma.user.update({
where: {
id: userId,
},
data: {
emailVerificationToken: code,
emailVerificationExpiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000), // 24 hours
},
});
await sendMailByTemplate(user.email, "email-verification", {
user: user,
code,
});
};