add: data validation for register api
This commit is contained in:
parent
d2ecdfab11
commit
34abe4582d
|
|
@ -7,7 +7,7 @@ import { hashPassword } from "../../utils/passwordUtils";
|
|||
import requestValidator from "../../utils/requestValidator";
|
||||
import authInfo from "../../middlewares/authInfo";
|
||||
import checkPermission from "../../middlewares/checkPermission";
|
||||
import { and, eq, isNull, ilike, or, sql } from "drizzle-orm";
|
||||
import { and, eq, or } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import HonoEnv from "../../types/HonoEnv";
|
||||
|
||||
|
|
@ -25,7 +25,6 @@ const registerFormSchema = z.object({
|
|||
|
||||
const respondentsRoute = new Hono<HonoEnv>()
|
||||
.use(authInfo)
|
||||
//create user and respondent
|
||||
.post(
|
||||
"/",
|
||||
checkPermission("register.create"),
|
||||
|
|
@ -33,6 +32,35 @@ const respondentsRoute = new Hono<HonoEnv>()
|
|||
async (c) => {
|
||||
const formData = c.req.valid("json");
|
||||
|
||||
// Build conditions based on available formData
|
||||
const conditions = [];
|
||||
if (formData.email) {
|
||||
conditions.push(eq(users.email, formData.email));
|
||||
}
|
||||
conditions.push(eq(users.username, formData.username));
|
||||
|
||||
const existingUser = await db
|
||||
.select()
|
||||
.from(users)
|
||||
.where(or(...conditions));
|
||||
|
||||
const existingRespondent = await db
|
||||
.select()
|
||||
.from(respondents)
|
||||
.where(eq(respondents.phoneNumber, formData.phoneNumber));
|
||||
|
||||
if (existingUser.length > 0) {
|
||||
throw new HTTPException(400, {
|
||||
message: "Email atau username sudah terdaftar",
|
||||
});
|
||||
}
|
||||
|
||||
if (existingRespondent.length > 0) {
|
||||
throw new HTTPException(400, {
|
||||
message: "Nomor HP sudah terdaftar",
|
||||
});
|
||||
}
|
||||
|
||||
// Hash the password
|
||||
const hashedPassword = await hashPassword(formData.password);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user