-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenuLogin.js
More file actions
76 lines (69 loc) · 2.59 KB
/
Copy pathmenuLogin.js
File metadata and controls
76 lines (69 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";
import fs from "node:fs/promises";
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) {
let ownRl = false;
if (!rl) {
rl = readline.createInterface({ input, output, terminal: true });
ownRl = true;
}
try {
while (true) {
console.log("\n---------------------------------------------------\n");
console.log("LOGIN");
console.log("1) Se connecter");
console.log("2) Créer un compte");
console.log("0) Quitter");
const choice = await rl.question("\nChoix > ");
if (choice === '1') {
const id = await rl.question("Identifiant : ");
rl.pause();
const question = [
{
type: "password",
name: "password",
message: "Mot de passe : ",
mask: '*',
}
];
const password = await inquirer.prompt(question);
rl.resume();
const data = await fs.readFile(path.join(process.cwd(), "actions", "account.json"), "utf-8");
const enseignants = JSON.parse(data);
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}`);
return user;
} else {
console.log(`${colors.red}\nIdentifiant ou mot de passe incorrect.${colors.reset}`);
continue;
}
}
else if (choice === '2') {
await ajoutAccount(rl);
continue;
}
else if (choice === '0') {
console.log("Vous avez quitté le logiciel.");
rl.close();
process.exit(0);
}
}
} catch (err) {
console.error("Erreur lors de la connexion :", err);
} finally {
if (ownRl && rl) {
rl.close();
}
}
}