basic register
This commit is contained in:
108
server.ts
108
server.ts
@@ -1,31 +1,115 @@
|
||||
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";
|
||||
|
||||
const app = new Hono();
|
||||
|
||||
app.use(
|
||||
"/api/v1/*",
|
||||
cors({
|
||||
origin: "*",
|
||||
allowHeaders: ["X-Custom-Header", "Upgrade-Insecure-Requests"],
|
||||
allowMethods: ["POST", "GET", "OPTIONS"],
|
||||
exposeHeaders: ["Content-Length", "X-Kuma-Revision"],
|
||||
maxAge: 600,
|
||||
credentials: true,
|
||||
}),
|
||||
);
|
||||
|
||||
app.get("/", async (c) => {
|
||||
const database = new DatabaseService();
|
||||
await database.getClient().user.create({
|
||||
data: {
|
||||
email: "rosati5.matteo@gmail.com",
|
||||
name: "Matteo",
|
||||
},
|
||||
});
|
||||
|
||||
const users = await database.getClient().user.findMany();
|
||||
|
||||
return c.json({
|
||||
message: "ok",
|
||||
users: users,
|
||||
});
|
||||
});
|
||||
|
||||
app.post("/api/v1/register", async (c) => {
|
||||
let body: RegisterRequest | undefined;
|
||||
let errors = false;
|
||||
let messages: Array<string> = [];
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
// //////////////////
|
||||
// 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(event, ws) {
|
||||
onOpen(e, ws) {
|
||||
console.log("Server: Connection opened");
|
||||
ws.send("Hello!");
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user