Skip to content

Commit

Permalink
FIX: Login
Browse files Browse the repository at this point in the history
  • Loading branch information
kauabarros-24 committed Sep 13, 2024
1 parent accf6b5 commit 11640c5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 41 deletions.
22 changes: 14 additions & 8 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 22 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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);
Expand All @@ -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);
Expand All @@ -48,7 +55,7 @@ io.on("connection", (socket) => {
});
});

server.listen(port, () => {
server.listen(3000, () => {
console.log(`Server running on port ${port}`);
});

Expand Down
12 changes: 7 additions & 5 deletions src/routes/user/user_authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
17 changes: 5 additions & 12 deletions src/routes/user/user_functions.ts
Original file line number Diff line number Diff line change
@@ -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");
});
Expand All @@ -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;

0 comments on commit 11640c5

Please sign in to comment.