From 11640c549862a88cc0a96fe7cbbfde4b0c359f98 Mon Sep 17 00:00:00 2001 From: kauabarros-24 Date: Fri, 13 Sep 2024 18:08:29 -0300 Subject: [PATCH] FIX: Login --- dist/index.js | 22 +++++++++------ dist/index.js.map | 2 +- src/index.ts | 37 +++++++++++++++----------- src/routes/user/user_authentication.ts | 12 +++++---- src/routes/user/user_functions.ts | 17 ++++-------- 5 files changed, 49 insertions(+), 41 deletions(-) diff --git a/dist/index.js b/dist/index.js index 12f9b20..cf8181b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,11 +1,10 @@ import door from "./routes/door/door_functions.js"; -import user from "./routes/user/user_functions.js"; import express from "express"; import cors from "cors"; -import bp from "body-parser"; import { Server } from "socket.io"; import { createServer } from "node:http"; import { loginUser } from "./routes/user/user_authentication.js"; +import teste from "./routes/user/user_functions.js"; const port = 8087; const app = express(); const server = createServer(app); @@ -14,22 +13,29 @@ const io = new Server(server, { origin: "*", }, }); +app.use(express.json()); +app.use('/', teste); app.post('/login', async (req, res) => { + require; try { - const { email, password } = req.body; + console.log(req.body); + const email = req.body.email; + const password = req.body.password; + console.log(password); + if (!email || !password) { + return res.status(400).json({ error: 'Email e senha são obrigatórios' }); + } + // Lógica de login vai aqui const { token } = await loginUser(email, password); - res.json({ token }); + return res.status(200).json(token); } catch (error) { res.status(400).json({ error: error.message }); } }); -app.use(bp.json()); -app.use(bp.urlencoded({ extended: true })); // allow all origins app.use(cors()); // Allow all origins app.use(door); -app.use(user); app.get("/", (req, res) => { console.log(req.ip); res.send("System working"); @@ -41,7 +47,7 @@ io.on("connection", (socket) => { console.log("user disconnected", socket.id); }); }); -server.listen(port, () => { +server.listen(3000, () => { console.log(`Server running on port ${port}`); }); export const updateFront = () => { diff --git a/dist/index.js.map b/dist/index.js.map index 6cac4ec..be4c89d 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA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 import door from "./routes/door/door_functions.js"; -import user from "./routes/user/user_functions.js"; import express from "express"; import cors from "cors"; -import bp from "body-parser"; import { Server } from "socket.io"; import { createServer } from "node:http"; import { loginUser } from "./routes/user/user_authentication.js"; + +import teste from "./routes/user/user_functions.js" +import bodyParser from "body-parser"; const port: number = 8087; const app = express(); const server = createServer(app); @@ -15,25 +16,31 @@ const io = new Server(server, { }, }); -app.post('/login', async (req: express.Request, res: express.Response) => { - try { - const {email, password } = req.body - const { token } = await loginUser(email, password) - res.json({ token }) +app.use(express.json()) - } - catch (error) { - res.status(400).json({error: error.message}) +app.use('/', teste) +app.post('/login', async (req, res) => {require + try { + console.log(req.body) + + const email = req.body.email + const password = req.body.password + console.log(password) + if (!email || !password) { + return res.status(400).json({ error: 'Email e senha são obrigatórios' }); + } + // Lógica de login vai aqui + const { token } = await loginUser(email, password); + return res.status(200).json(token) + } catch (error) { + res.status(400).json({ error: error.message }); } -}) +}); -app.use(bp.json()); -app.use(bp.urlencoded({ extended: true })); // allow all origins app.use(cors()); // Allow all origins app.use(door); -app.use(user); app.get("/", (req: express.Request, res: express.Response) => { console.log(req.ip); @@ -48,7 +55,7 @@ io.on("connection", (socket) => { }); }); -server.listen(port, () => { +server.listen(3000, () => { console.log(`Server running on port ${port}`); }); diff --git a/src/routes/user/user_authentication.ts b/src/routes/user/user_authentication.ts index a205483..38badae 100644 --- a/src/routes/user/user_authentication.ts +++ b/src/routes/user/user_authentication.ts @@ -4,23 +4,25 @@ import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); -const SECRET_KEY = 'your-secret-key'; +const SECRET_KEY = 'manoPotassio'; export function generateToken(userId: number): string { return jwt.sign({ userId }, SECRET_KEY, { expiresIn: '1h' }); } -export async function registerUser(email: string, password: string) { +export async function registerUser(email: string, password: string, name:string) { const hashedPassword = await bcrypt.hash(password, 10); - const user = await prisma.userLogin.create({ + const user = await prisma.user.create({ data: { email, password: hashedPassword, + name, }, }); return user; -}export async function loginUser(email: string, password: string) { - const user = await prisma.userLogin.findUnique({ where: { email } }); +} +export async function loginUser(email: string, password: string) { + const user = await prisma.user.findUnique({ where: { email } }); if (!user) { throw new Error('Usuário não encontrado'); } diff --git a/src/routes/user/user_functions.ts b/src/routes/user/user_functions.ts index 83a7f2c..a42876b 100644 --- a/src/routes/user/user_functions.ts +++ b/src/routes/user/user_functions.ts @@ -1,12 +1,13 @@ import express from "express"; -import { listAllUser, createUser, isUserSynced } from "../../db/db.js"; +import { listAllUser, createUser} from "../../db/db.js"; const router = express.Router(); -router.post("/create/", (req: express.Request, res: express.Response) => { +router.post("/create/", async (req: express.Request, res: express.Response) => { const name: string = req.body.name; const email: string = req.body.email; + const password: string = req.body.password; - createUser(name, email); + await createUser(name, email, password); res.send("User created"); }); @@ -25,12 +26,4 @@ router.get("/users/", async (req, res) => { // res.send(`${rfid} assigned to ${email}`); // }); -router.post("/sync/", async (req: express.Request, res: express.Response) => { - const name: string = req.body.name; - - await isUserSynced(name); - - res.send(`${name} synced`); -}); - -export default router; +export default router; \ No newline at end of file