Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions actions/account.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"id": "pierre",
"password": "1234",
"password": "$2b$10$I1FEg.qONWiSG8P8AbjM9eplDluAgyIkkxq/dkvkRd.146Zq60dom",
"role": "enseignant",
"prenom": "Pierre",
"nom": "Dupont",
Expand All @@ -11,12 +11,12 @@
"rue": "12 rue Marie Curie",
"ville": "Troyes",
"region": "Grand-Est",
"code_postal": "10000"
"codePostal": "10000"
}
},
{
"id": "jean",
"password": "2345",
"password": "$2b$10$uMzzmySq4xaGExeQSV4.CuJBlmOb3n.WoxdyeVsA77n8J07jbPXIO",
"role": "etudiant",
"prenom": "Jean",
"nom": "Dupont",
Expand All @@ -26,7 +26,7 @@
"rue": "13 rue Marie Curie",
"ville": "Troyes",
"region": "Grand-Est",
"code_postal": "10000"
"codePostal": "10000"
}
}
]
3 changes: 2 additions & 1 deletion actions/ajoutAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { stdin as input, stdout as output } from "node:process";
import path from "node:path";
import fs from "node:fs/promises";
import { colors } from '../utils/colors.js';
import {hashPassword} from './passwordManager.js';

export default async function ajoutAccount(rl = null) {
console.log("\n Ajout d'un compte : \n");
Expand All @@ -19,7 +20,7 @@ export default async function ajoutAccount(rl = null) {
const accounts = JSON.parse(data);

const id = await rl.question("Identifiant : ");
const password = await rl.question("Mot de passe : ");
const password = await hashPassword(await rl.question("Mot de passe : "));
const role = await demanderRole(rl);
const prenom = await rl.question("Prenom : ");
const nom = await rl.question("Nom : ");
Expand Down
13 changes: 13 additions & 0 deletions actions/passwordManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import bcrypt from 'bcryptjs';

export async function hashPassword(password) {
const salt = await bcrypt.genSalt(10);

const hashedPassword = await bcrypt.hash(password, salt);
return hashedPassword;
}

export async function checkPassword(plainPassword, hashedPassword) {
const isMatch = await bcrypt.compare(plainPassword, hashedPassword);
return isMatch;
}
9 changes: 7 additions & 2 deletions menuLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from "node:path";
import inquirer from "inquirer";
import ajoutAccount from "./actions/ajoutAccount.js";
import { colors } from "./utils/colors.js";
import {checkPassword} from "./actions/passwordManager.js";

export default async function menuLogin(rl = null) {

Expand Down Expand Up @@ -40,8 +41,12 @@ export default async function menuLogin(rl = null) {
const data = await fs.readFile(path.join(process.cwd(), "actions", "account.json"), "utf-8");
const enseignants = JSON.parse(data);

const user = enseignants.find(e => e.id === id && e.password === password.password);

let user;
for (const e of enseignants) {
if (e.id === id && await checkPassword(password.password, e.password)) {
user = e;
}
}

if (user) {
console.log(`${colors.green}\nConnexion réussie ! Bienvenue ${user.prenom} ${user.nom}.${colors.reset}`);
Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"@caporal/core": "^2.0.2",
"bcryptjs": "^3.0.3",
"colors": "^1.4.0",
"inquirer": "^12.11.1",
"prompt": "^1.2.0",
Expand Down