All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 0s
55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
import { passwordSchema, userSchema } from "@/lib/validation/zod/user";
|
|
import { createTRPCRouter, publicProcedure } from "../trpc";
|
|
import { z } from "zod";
|
|
import { eq } from "drizzle-orm";
|
|
import { users } from "@/server/db/schema";
|
|
import argon from "argon2";
|
|
|
|
export const authRouter = createTRPCRouter({
|
|
register: publicProcedure
|
|
.input(
|
|
z.object({
|
|
user: userSchema,
|
|
password: passwordSchema,
|
|
}),
|
|
)
|
|
.mutation(async ({ ctx, input }) => {
|
|
const {
|
|
password,
|
|
user: { email, name },
|
|
} = input;
|
|
// Check if user already exists
|
|
try {
|
|
const existingUser = await ctx.db.query.users.findFirst({
|
|
where: eq(users.email, email),
|
|
});
|
|
|
|
if (existingUser) {
|
|
return { success: false, message: "User already exists" };
|
|
}
|
|
|
|
// Hash the password (12 is a good cost factor)
|
|
const hashedPassword = await argon.hash(password);
|
|
|
|
// Create user in database
|
|
const [user] = await ctx.db
|
|
.insert(users)
|
|
.values({
|
|
name,
|
|
email,
|
|
password: hashedPassword,
|
|
})
|
|
.returning({ id: users.id });
|
|
console.log(user);
|
|
|
|
if (user) {
|
|
return { success: true, message: "User created successfully" };
|
|
}
|
|
return { success: false, message: "Error creating user" };
|
|
} catch (e) {
|
|
console.error(e);
|
|
return { success: false, message: "Error creating user" };
|
|
}
|
|
}),
|
|
});
|