Loading hono-postgres-setup/server.ts +6 −6 Original line number Diff line number Diff line import { Hono } from "hono"; import { serve } from "@hono/node-server"; import { userRoutes } from "./src/user/user.route"; import { authRoutes } from "./src/auth/auth.route"; import { userRoutes } from "./src/user/user.router"; const app = new Hono(); // Register the routes authRoutes(app); userRoutes(app); serve({ Loading @@ -12,10 +14,8 @@ serve({ port: 8000, }); serve(app, (info: { port: number }) => { console.log(`Listening on http://localhost:${info.port}`); }); export default app No newline at end of file export default app; hono-postgres-setup/src/auth/auth.route.ts +3 −3 Original line number Diff line number Diff line Loading @@ -4,15 +4,15 @@ import { loginUser, passwordReset, registerUser, } from "./user.controller"; } from "./auth.controller"; import { forgotPasswordSchema, loginSchema, passwordResetSchema, registrationSchema, } from "./user.validation.schema"; } from "./auth.validation.schema"; export const userRoutes = (app: Hono) => { export const authRoutes = (app: Hono) => { app.post("/user/register", async (c) => { const body = await c.req.json(); Loading hono-postgres-setup/src/user/user.controller.ts 0 → 100644 +44 −0 Original line number Diff line number Diff line import { Context } from "hono"; import db from "../../db/db.connect"; import { users } from "../models/user.model"; import { role } from "../models/role.model"; import { eq } from "drizzle-orm"; import { hashPassword } from "../auth/password.utill"; export const createUser = async (c: Context) => { const body = await c.req.json(); const email = body.email.trim(); const existingUser = await db .select() .from(users) .where(eq(users.email, email)); if (existingUser.length > 0) { return c.json({ error: `Email "${email}" already exists!` }, 400); } const hashedPassword = await hashPassword(body.password); const newUser = { firstName: body.firstName, lastName: body.lastName, email: email, phoneNumber: body.phoneNumber, password: hashedPassword, createdAt: new Date(), }; const [insertedUser] : any = await db.insert(users).values(newUser).returning(); const profile = { firstName: insertedUser.firstName, lastName: insertedUser.lastName, phoneNumber: insertedUser.phoneNumber, status: insertedUser.status, entityId: insertedUser.entityId }; return c.json(profile, 201); }; hono-postgres-setup/src/user/user.router.ts 0 → 100644 +17 −0 Original line number Diff line number Diff line import { Hono } from "hono"; import { createUser, } from "../user/user.controller" export const userRoutes = (app: Hono) => { app.post("/user/create", async (c) => { const body = await c.req.json(); try { } catch (error: any) { return c.json({ error: error.errors }, 400); } return createUser(c); }); } No newline at end of file Loading
hono-postgres-setup/server.ts +6 −6 Original line number Diff line number Diff line import { Hono } from "hono"; import { serve } from "@hono/node-server"; import { userRoutes } from "./src/user/user.route"; import { authRoutes } from "./src/auth/auth.route"; import { userRoutes } from "./src/user/user.router"; const app = new Hono(); // Register the routes authRoutes(app); userRoutes(app); serve({ Loading @@ -12,10 +14,8 @@ serve({ port: 8000, }); serve(app, (info: { port: number }) => { console.log(`Listening on http://localhost:${info.port}`); }); export default app No newline at end of file export default app;
hono-postgres-setup/src/auth/auth.route.ts +3 −3 Original line number Diff line number Diff line Loading @@ -4,15 +4,15 @@ import { loginUser, passwordReset, registerUser, } from "./user.controller"; } from "./auth.controller"; import { forgotPasswordSchema, loginSchema, passwordResetSchema, registrationSchema, } from "./user.validation.schema"; } from "./auth.validation.schema"; export const userRoutes = (app: Hono) => { export const authRoutes = (app: Hono) => { app.post("/user/register", async (c) => { const body = await c.req.json(); Loading
hono-postgres-setup/src/user/user.controller.ts 0 → 100644 +44 −0 Original line number Diff line number Diff line import { Context } from "hono"; import db from "../../db/db.connect"; import { users } from "../models/user.model"; import { role } from "../models/role.model"; import { eq } from "drizzle-orm"; import { hashPassword } from "../auth/password.utill"; export const createUser = async (c: Context) => { const body = await c.req.json(); const email = body.email.trim(); const existingUser = await db .select() .from(users) .where(eq(users.email, email)); if (existingUser.length > 0) { return c.json({ error: `Email "${email}" already exists!` }, 400); } const hashedPassword = await hashPassword(body.password); const newUser = { firstName: body.firstName, lastName: body.lastName, email: email, phoneNumber: body.phoneNumber, password: hashedPassword, createdAt: new Date(), }; const [insertedUser] : any = await db.insert(users).values(newUser).returning(); const profile = { firstName: insertedUser.firstName, lastName: insertedUser.lastName, phoneNumber: insertedUser.phoneNumber, status: insertedUser.status, entityId: insertedUser.entityId }; return c.json(profile, 201); };
hono-postgres-setup/src/user/user.router.ts 0 → 100644 +17 −0 Original line number Diff line number Diff line import { Hono } from "hono"; import { createUser, } from "../user/user.controller" export const userRoutes = (app: Hono) => { app.post("/user/create", async (c) => { const body = await c.req.json(); try { } catch (error: any) { return c.json({ error: error.errors }, 400); } return createUser(c); }); } No newline at end of file