complete registration and login

This commit is contained in:
Matteo Rosati
2026-01-26 14:44:17 +01:00
parent 4fc146789f
commit c9db7c89d8
15 changed files with 315 additions and 114 deletions

123
server.ts
View File

@@ -1,12 +1,8 @@
import { Hono } from "hono";
import { cors } from "hono/cors";
import { upgradeWebSocket, websocket } from "hono/bun";
import { DatabaseService } from "@/services/database-service";
import { RegisterRequest, RegisterResponse } from "@/types/types";
import { MESSAGES } from "@/auth/messages";
import { validateEmail } from "@/utilities/email";
import { Prisma } from "@/orm/generated/prisma/client";
import { hashPassword } from "@/utilities/password";
// import { upgradeWebSocket, websocket } from "hono/bun";
import { RegisterController } from "@/controllers/register";
import { LoginController } from "@/controllers/login";
const app = new Hono();
@@ -28,103 +24,30 @@ app.get("/", async (c) => {
});
});
app.post("/api/v1/register", async (c) => {
let body: RegisterRequest | undefined;
let errors = false;
let messages: Array<string> = [];
app.post("/api/v1/register", RegisterController);
// Get the request body and handle malformed payload
try {
body = (await c.req.json()) as RegisterRequest;
} catch (error) {
console.error(`Received invalid payload: ${error}`);
return c.json({
status: "error",
messages: [MESSAGES.INVALID_REQUEST],
} as RegisterResponse);
}
app.post("/api/v1/login", LoginController);
// //////////////////
// Request validation
if (!body.email) {
errors = true;
messages.push(MESSAGES.MISSING_EMAIL);
}
if (!validateEmail(body.email)) {
errors = true;
messages.push(MESSAGES.INVALID_EMAIL);
}
if (!body.password) {
errors = true;
messages.push(MESSAGES.MISSING_PASSWORD);
}
// End: Request validation
// ///////////////////////
if (errors) {
return c.json({
status: "error",
messages: messages,
} as RegisterResponse);
}
// Database
const database = new DatabaseService();
try {
// Sala la password
body.password = await hashPassword(body.password);
await database.getClient().user.create({
data: {
...body,
first_login: true,
},
});
} catch (e) {
if (
e instanceof Prisma.PrismaClientKnownRequestError &&
e.code === "P2002"
) {
return c.json({
status: "error",
messages: [MESSAGES.USER_ALREADY_EXISTS],
} as RegisterResponse);
}
return c.json({
status: "error",
messages: [MESSAGES.UNKNOWN_DATABASE_ERROR],
} as RegisterResponse);
}
return c.json({
status: "success",
} as RegisterResponse);
});
app.get(
"/ws",
upgradeWebSocket((c) => {
return {
onOpen(e, ws) {
console.log("Server: Connection opened");
ws.send("Hello!");
},
onClose: () => {
console.log("Server: Connection closed");
},
onMessage: (ev) => {
console.log(ev.data);
},
};
}),
);
// app.get(
// "/ws",
// upgradeWebSocket((c) => {
// return {
// onOpen(e, ws) {
// console.log("Server: Connection opened");
// ws.send("Hello!");
// },
// onClose: () => {
// console.log("Server: Connection closed");
// },
// onMessage: (ev) => {
// console.log(ev.data);
// },
// };
// }),
// );
export default {
port: Bun.env.SERVER_PORT,
fetch: app.fetch,
websocket,
// websocket,
};