feat: implement credentials login
Some checks failed
container-scan / Container Scan (pull_request) Failing after 5m8s
docker-build / docker (pull_request) Failing after 7m31s

implements the credentials login functionality
This commit is contained in:
Dominik 2025-05-28 13:08:07 +02:00
parent cdc0e81e51
commit eb0f8d8d5f
Signed by: dominik
GPG key ID: 06A4003FC5049644
10 changed files with 321 additions and 35 deletions

60
src/register.ts Normal file
View file

@ -0,0 +1,60 @@
"use server";
import type { z } from "zod";
import bcrypt from "bcryptjs";
import { registerSchema } from "./validation";
import { prisma } from "@/prisma";
export async function registerAction(data: z.infer<typeof registerSchema>) {
try {
const result = await registerSchema.safeParseAsync(data);
if (!result.success) {
return {
error: result.error.errors[0].message,
};
}
const { email, password, username } = result.data;
const user = await prisma.user.findUnique({
where: {
email,
},
});
if (user) {
return {
error: "User already exist with this email",
};
}
const passwordHash = await bcrypt.hash(password, 10);
await prisma.$transaction(async (tx) => {
const { id } = await tx.user.create({
data: {
email,
name: username,
password_hash: passwordHash,
},
});
await tx.account.create({
data: {
userId: id,
type: "credentials",
provider: "credentials",
providerAccountId: id,
},
});
});
return {};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (_error) {
return {
error: "System error. Please contact support",
};
}
}