- Added function to password for encrypt it
    - Doing function for create account
This commit is contained in:
edbernar
2024-08-08 17:27:23 +02:00
parent cdbff66aa8
commit bce43a609a
8 changed files with 83 additions and 23 deletions

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* createConnectDiv.js :+: :+: :+: */ /* createConnectDiv.js :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */ /* By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/07 18:14:53 by edbernar #+# #+# */ /* Created: 2024/08/07 18:14:53 by edbernar #+# #+# */
/* Updated: 2024/08/07 22:33:39 by edbernar ### ########.fr */ /* Updated: 2024/08/08 17:27:14 by edbernar ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,12 +14,13 @@ import { userMeInfo, waitForLogin } from "../typeResponse/typeLogin.js";
import { createNotification as CN } from "../notification/main.js"; import { createNotification as CN } from "../notification/main.js";
import { sendRequest } from "../websocket.js"; import { sendRequest } from "../websocket.js";
function createConnectDiv() function createConnectDiv(divLogin)
{ {
const divConnect = document.createElement("div"); const divConnect = document.createElement("div");
const inputLogin = document.createElement("input"); const inputLogin = document.createElement("input");
const inputPass = document.createElement("input"); const inputPass = document.createElement("input");
const buttonLogin = createButton(inputLogin, inputPass); const buttonLogin = createButton(inputLogin, inputPass);
const buttonNewAcc = createButtonNewAcc(divConnect, divLogin);
addGlobalBg(); addGlobalBg();
divConnect.setAttribute("id", "connectDiv"); divConnect.setAttribute("id", "connectDiv");
@ -31,6 +32,7 @@ function createConnectDiv()
divConnect.appendChild(inputLogin); divConnect.appendChild(inputLogin);
divConnect.appendChild(inputPass); divConnect.appendChild(inputPass);
divConnect.appendChild(buttonLogin); divConnect.appendChild(buttonLogin);
divConnect.appendChild(buttonNewAcc);
return (divConnect); return (divConnect);
} }
@ -42,15 +44,29 @@ function addGlobalBg()
document.body.appendChild(globalBg); document.body.appendChild(globalBg);
} }
async function hashPassword(password)
{
const encoder = new TextEncoder();
const data = encoder.encode(password);
const hash = await crypto.subtle.digest('SHA-256', data);
const hashArray = Array.from(new Uint8Array(hash));
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
return (hashHex);
}
function createButton(inputLogin, inputPass) function createButton(inputLogin, inputPass)
{ {
const loginButton = document.getElementById('loginButton'); const loginButton = document.getElementById('loginButton');
const pLoginButton = loginButton.getElementsByTagName('p')[0]; const pLoginButton = loginButton.getElementsByTagName('p')[0];
const button = document.createElement("button"); const button = document.createElement("button");
let usernameNode = null; let usernameNode = null;
let hashedPass = null;
button.addEventListener('click', () => { button.addEventListener('click', () => {
sendRequest("login", {type: "byPass", mail: inputLogin.value, password: inputPass.value}); hashPassword(inputPass.value).then((hash) => {
hashedPass = hash;
sendRequest("login", {type: "byPass", mail: inputLogin.value, password: hashedPass});
waitForLogin().then((token) => { waitForLogin().then((token) => {
usernameNode = document.createTextNode(userMeInfo.username); usernameNode = document.createTextNode(userMeInfo.username);
loginButton.replaceChild(usernameNode, pLoginButton); loginButton.replaceChild(usernameNode, pLoginButton);
@ -59,8 +75,51 @@ function createButton(inputLogin, inputPass)
document.getElementById("globalBg").remove(); document.getElementById("globalBg").remove();
document.cookie = "token={" + token + "}; path=/; Secure; SameSite=Strict; max-age=3600"; document.cookie = "token={" + token + "}; path=/; Secure; SameSite=Strict; max-age=3600";
}); });
}).catch((err) => {
CN.new("Error", "An error occured while trying to connect", CN.defaultIcon.error);
});
}); });
return (button); return (button);
} }
function createButtonNewAcc(divConnect, divLogin)
{
const button = document.createElement("button");
const newDiv = document.createElement("div");
const inputUsername = document.createElement("input");
const inputMail = document.createElement("input");
const inputPass = document.createElement("input");
const buttonCreate = document.createElement("button");
button.innerHTML = "Create a new account";
newDiv.setAttribute("id", "connectDiv");
button.addEventListener('click', () => {
inputUsername.setAttribute("type", "text");
inputUsername.setAttribute("placeholder", "username");
inputMail.setAttribute("type", "text");
inputMail.setAttribute("placeholder", "mail");
inputPass.setAttribute("type", "password");
inputPass.setAttribute("placeholder", "password");
buttonCreate.innerHTML = "Create";
newDiv.appendChild(inputUsername);
newDiv.appendChild(inputMail);
newDiv.appendChild(inputPass);
newDiv.appendChild(buttonCreate);
button.addEventListener('click', createNewAccount);
divConnect.remove();
divLogin.appendChild(newDiv);
});
return (button);
}
function createNewAccount()
{
const inputUsername = document.getElementsByTagName("input")[0];
const inputMail = document.getElementsByTagName("input")[1];
const inputPass = document.getElementsByTagName("input")[2];
console.warn("Faire la requete pour creer un compte");
}
export { createConnectDiv }; export { createConnectDiv };

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* main.js :+: :+: :+: */ /* main.js :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */ /* By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/07 17:40:15 by edbernar #+# #+# */ /* Created: 2024/08/07 17:40:15 by edbernar #+# #+# */
/* Updated: 2024/08/07 22:31:31 by edbernar ### ########.fr */ /* Updated: 2024/08/08 17:07:12 by edbernar ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -35,7 +35,7 @@ function showLoginDiv()
{ {
const divLogin = document.createElement("div"); const divLogin = document.createElement("div");
const threeDiv = createThreeDiv(); const threeDiv = createThreeDiv();
const connectDiv = createConnectDiv(); const connectDiv = createConnectDiv(divLogin);
divLogin.setAttribute("id", "loginDiv"); divLogin.setAttribute("id", "loginDiv");
divLogin.appendChild(threeDiv); divLogin.appendChild(threeDiv);

View File

@ -3,10 +3,10 @@
# ::: :::::::: # # ::: :::::::: #
# login.py :+: :+: :+: # # login.py :+: :+: :+: #
# +:+ +:+ +:+ # # +:+ +:+ +:+ #
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ # # By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/08/03 08:10:38 by edbernar #+# #+# # # Created: 2024/08/03 08:10:38 by edbernar #+# #+# #
# Updated: 2024/08/07 22:21:58 by edbernar ### ########.fr # # Updated: 2024/08/08 16:42:43 by edbernar ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -25,13 +25,14 @@ userList = [
"token": "IDSNCSDAd465sd13215421", "token": "IDSNCSDAd465sd13215421",
"mail": "eddy@ediwor.fr", "mail": "eddy@ediwor.fr",
"password": "ABC123", "password": "ABC123",
"id": 9999999 "id": 9999999,
'id42': 123456
}, },
{ {
"username": "Eddy", "username": "Eddy",
"token": "54dsadw8f4a6w5f4a62s4f984fa62f4as65", "token": "54dsadw8f4a6w5f4a62s4f984fa62f4as65",
"mail": "aaaaa", "mail": "aaaaa",
"password": "aaaaa", "password": "ed968e840d10d2d313a870bc131a4e2c311d7ad09bdf32b3418147221f51a6e2", # not hashed : aaaaa
"id": 2135421 "id": 2135421
}, },
{ {
@ -45,7 +46,7 @@ userList = [
"username": "Mathis", "username": "Mathis",
"token": "8cb1qjlfndc12mn2l1mn654xzkkhad54cxz", "token": "8cb1qjlfndc12mn2l1mn654xzkkhad54cxz",
"mail": "ccccc", "mail": "ccccc",
"password": "ccccc", "password": "6304fbfe2b22557c34c42a70056616786a733b3d09fb326308c813d6ab712ec0", # not hashed : ccccc
"id": 2371234 "id": 2371234
}, },
{ {