Skip to content

Commit b4b900b

Browse files
committed
feat: added the two endpoints for create user and login user
1 parent f003fdb commit b4b900b

File tree

15 files changed

+313
-17
lines changed

15 files changed

+313
-17
lines changed

package-lock.json

Lines changed: 83 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ node_modules
33
.env
44

55
/src/generated/prisma
6+
/prisma/dev.db
7+
/prisma/dev.db-journal

server/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010
"author": "",
1111
"license": "ISC",
1212
"dependencies": {
13+
"@prisma/client": "^6.13.0",
14+
"bcrypt": "^6.0.0",
1315
"cors": "^2.8.5",
14-
"express": "^5.1.0"
16+
"express": "^5.1.0",
17+
"zod": "^4.0.13"
1518
},
1619
"devDependencies": {
20+
"@types/bcrypt": "^6.0.0",
1721
"@types/cors": "^2.8.19",
1822
"@types/express": "^5.0.3",
1923
"@types/node": "^24.1.0",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- CreateTable
2+
CREATE TABLE "Users" (
3+
"id_user" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
4+
"name" TEXT NOT NULL,
5+
"email" TEXT NOT NULL,
6+
"password" TEXT NOT NULL
7+
);
8+
9+
-- CreateIndex
10+
CREATE UNIQUE INDEX "Users_email_key" ON "Users"("email");
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Please do not edit this file manually
2+
# It should be added in your version-control system (e.g., Git)
3+
provider = "sqlite"

server/src/db/connect.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { PrismaClient } from "../generated/prisma"
2+
3+
const prisma = new PrismaClient()
4+
5+
export default prisma

server/src/db/models/UsersModel.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import z from "zod";
2+
3+
export const fullSchema = z.object({
4+
idUser: z.number().int().optional(),
5+
name: z.string().min(3, "INVALID_NAME"),
6+
email: z.email("INVALID_EMAIL"),
7+
password: z.string().min(4, "PWD_MIN_CHAR")
8+
})
9+
10+
export const loginSchema = fullSchema.omit({ idUser: true, name: true })
11+
12+
export type CreateUserDTO = z.infer<typeof fullSchema>
13+
export type LoginUser = Omit<CreateUserDTO, "password" | "idUser">
14+
export type PublicUserDTO = Omit<CreateUserDTO, "password">

server/src/errors/HttpError.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export class HttpError extends Error {
2+
statusCode: number
3+
errorCode: string
4+
5+
constructor(message: string, statusCode: number, errorCode: string) {
6+
super(message)
7+
this.name = "HttpError"
8+
this.statusCode = statusCode
9+
this.errorCode = errorCode
10+
}
11+
}
12+
13+
export class CustomError extends HttpError {
14+
constructor(message: string, statusCode: number, errorCode: string) {
15+
super(message, statusCode, errorCode)
16+
}
17+
}
18+
19+
export class InvalidRequest extends HttpError {
20+
errors: {
21+
fields: string
22+
message: string
23+
}[]
24+
constructor(message: string, statusCode: number, errorCode: string, errors: {
25+
fields: string
26+
message: string
27+
}[]) {
28+
super(message, statusCode, errorCode)
29+
this.errors = errors
30+
}
31+
}

server/src/http/server.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import express, { Response, Request } from 'express'
1+
import express from 'express'
22
import cors from "cors"
3+
import { errorHandler } from '../middleware/errorHandler'
4+
import UserRoutes from '../routes/UserRoutes'
35

46

57
const app = express()
68

79
app.use(cors())
810
app.use(express.json())
911

10-
app.get('/', (req: Request, res: Response) => {
11-
res.status(200).json({
12-
message: 'funcionou'
13-
})
14-
})
12+
app.use(UserRoutes)
13+
14+
app.use(errorHandler)
1515

1616
export default app
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Request, Response, NextFunction } from "express";
2+
import { HttpError } from "../errors/HttpError";
3+
4+
5+
export function errorHandler(err: unknown, req: Request, res: Response, next: NextFunction) {
6+
if (err instanceof HttpError) {
7+
return res.status(err.statusCode).json({
8+
error: err.message,
9+
code: err.errorCode
10+
})
11+
}
12+
13+
console.error("Unexpected error. " + err)
14+
return res.status(500).json({ error: "Server error." })
15+
}

0 commit comments

Comments
 (0)