|
|
|
@ -5,6 +5,7 @@ import { Type, TypeBoxTypeProvider } from "@fastify/type-provider-typebox";
|
|
|
|
|
import websocket, { WebSocket } from "@fastify/websocket";
|
|
|
|
|
import bcrypt from "bcrypt";
|
|
|
|
|
import Fastify, { FastifyReply } from "fastify";
|
|
|
|
|
import fastifyJwt from "@fastify/jwt";
|
|
|
|
|
import { nanoid } from "nanoid";
|
|
|
|
|
import { allocateBuffer, getRunner } from "runner";
|
|
|
|
|
import { Pull, Push } from "zeromq";
|
|
|
|
@ -115,6 +116,11 @@ const database = db.openDatabase();
|
|
|
|
|
/* Créer les tables si elles n'existent pas */
|
|
|
|
|
db.createTables(database);
|
|
|
|
|
|
|
|
|
|
fastify.register(fastifyJwt, {
|
|
|
|
|
secret: 'supersecret' // Utilisez une clé secrète sécurisée en production
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const salt = 10;
|
|
|
|
|
|
|
|
|
|
/* Route pour créer un utilisateur */
|
|
|
|
@ -139,7 +145,7 @@ fastify.post(
|
|
|
|
|
if (!(await db.insertUser(database, login, hash, permissions))) {
|
|
|
|
|
reply.send({ success: false });
|
|
|
|
|
} else {
|
|
|
|
|
reply.send({ success: true });
|
|
|
|
|
return reply.send({ success: true });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
@ -159,10 +165,18 @@ fastify.post(
|
|
|
|
|
async (request, reply) => {
|
|
|
|
|
const { login, password } = request.body;
|
|
|
|
|
const user = await db.verifyUser(database, login);
|
|
|
|
|
if (!user) {
|
|
|
|
|
return reply.code(401).send({ error: 'Invalid username or password' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const isPasswordValid = await bcrypt.compare(password, user.password);
|
|
|
|
|
if(isPasswordValid){
|
|
|
|
|
const token = fastify.jwt.sign({ login });
|
|
|
|
|
reply.send({ token: token });
|
|
|
|
|
} else {
|
|
|
|
|
reply.code(401).send({ error: 'Invalid username or password' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bcrypt.compare(password, user?.password)
|
|
|
|
|
.then(res => reply.send({ sucess: res }))
|
|
|
|
|
.catch(err => reply.send({ sucess: false }));
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|