début hashage
continuous-integration/drone/push Build is passing Details

Hugo PRADIER 11 months ago
parent 9bfd03b19a
commit 04f9fd9c04

@ -7,6 +7,7 @@
"start": "tsx src/server.ts"
},
"devDependencies": {
"@types/bcryptjs": "^2.4.6",
"@types/bun": "^1.0.4",
"tsx": "^4.7.0",
"typescript": "^5.3.3"
@ -18,6 +19,7 @@
"@fastify/type-provider-typebox": "^4.0.0",
"@fastify/websocket": "^10.0.1",
"@sinclair/typebox": "^0.32.9",
"bcryptjs": "^2.4.3",
"fastify": "^4.27.0",
"nanoid": "^5.0.4",
"sqlite3": "^5.1.7",

@ -0,0 +1,18 @@
import * as bcrypt from "bcryptjs";
const saltRounds = 10; // Le nombre de tours de salage
/* Fonction pour hasher le mot de passe */
export async function hashPassword(password: string): Promise<string> {
const hashedPassword = await bcrypt.hash(password, saltRounds);
return hashedPassword;
}
/* Fonction pour vérifier le mot de passe */
export async function comparePassword(
plainPassword: string,
hashedPassword: string
): Promise<boolean> {
const isMatch = await bcrypt.compare(plainPassword, hashedPassword);
return isMatch;
}

@ -8,6 +8,7 @@ import { Pull, Push } from "zeromq";
import { ChangeSet, Text } from "@codemirror/state";
import { Update, rebaseUpdates } from "@codemirror/collab";
import * as db from "./database";
import { hashPassword } from "bcrypt";
const sender = new Push();
await sender.bind(`tcp://127.0.0.1:5557`);
@ -128,7 +129,9 @@ fastify.post(
},
async (request, reply) => {
const { login, password, permissions } = request.body;
if (!(await db.insertUser(database, login, password, permissions))) {
// Hasher le mot de passe avant de l'insérer dans la base de données (en type string)
const hashedPassword = (await hashPassword(password)) as string;
if (!(await db.insertUser(database, login, hashedPassword, permissions))) {
reply.send({ success: false });
} else {
reply.send({ success: true });

Loading…
Cancel
Save