Site
- Added cookie for keep token - Added username in login button if user is connected
This commit is contained in:
@ -3,14 +3,15 @@
|
||||
/* ::: :::::::: */
|
||||
/* createConnectDiv.js :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ */
|
||||
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/07 18:14:53 by edbernar #+# #+# */
|
||||
/* Updated: 2024/08/07 19:09:41 by edbernar ### ########.fr */
|
||||
/* Updated: 2024/08/07 22:33:39 by edbernar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
import { createNotification } from "../notification/main.js";
|
||||
import { userMeInfo, waitForLogin } from "../typeResponse/typeLogin.js";
|
||||
import { createNotification as CN } from "../notification/main.js";
|
||||
import { sendRequest } from "../websocket.js";
|
||||
|
||||
function createConnectDiv()
|
||||
@ -43,10 +44,21 @@ function addGlobalBg()
|
||||
|
||||
function createButton(inputLogin, inputPass)
|
||||
{
|
||||
const loginButton = document.getElementById('loginButton');
|
||||
const pLoginButton = loginButton.getElementsByTagName('p')[0];
|
||||
const button = document.createElement("button");
|
||||
let usernameNode = null;
|
||||
|
||||
button.addEventListener('click', () => {
|
||||
sendRequest("login", {type: "byPass", mail: inputLogin.value, password: inputPass.value});
|
||||
waitForLogin().then((token) => {
|
||||
usernameNode = document.createTextNode(userMeInfo.username);
|
||||
loginButton.replaceChild(usernameNode, pLoginButton);
|
||||
CN.new("Connected successfully", "Welcome " + userMeInfo.username, CN.defaultIcon.success);
|
||||
document.getElementById("loginDiv").remove();
|
||||
document.getElementById("globalBg").remove();
|
||||
document.cookie = "token={" + token + "}; path=/; Secure; SameSite=Strict; max-age=3600";
|
||||
});
|
||||
});
|
||||
return (button);
|
||||
}
|
||||
|
@ -3,22 +3,32 @@
|
||||
/* ::: :::::::: */
|
||||
/* main.js :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ */
|
||||
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/07 17:40:15 by edbernar #+# #+# */
|
||||
/* Updated: 2024/08/07 18:38:29 by edbernar ### ########.fr */
|
||||
/* Updated: 2024/08/07 22:31:31 by edbernar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
import { createNotification as CN } from "../notification/main.js";
|
||||
import { userMeInfo, waitForLogin } from "../typeResponse/typeLogin.js";
|
||||
import { createConnectDiv } from "./createConnectDiv.js";
|
||||
import { createThreeDiv } from "./createThreeDiv.js";
|
||||
|
||||
function login()
|
||||
{
|
||||
const loginButton = document.getElementById('loginButton');
|
||||
const pLoginButton = loginButton.getElementsByTagName('p')[0];
|
||||
let nodeText = null;
|
||||
|
||||
waitForLogin().then(() => {
|
||||
nodeText = document.createTextNode(userMeInfo.username);
|
||||
|
||||
if (userMeInfo.id !== -1)
|
||||
loginButton.replaceChild(nodeText, pLoginButton);
|
||||
else
|
||||
loginButton.addEventListener('click', showLoginDiv);
|
||||
});
|
||||
}
|
||||
|
||||
function showLoginDiv()
|
||||
|
@ -0,0 +1,20 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* typeErrorInvalidPassword.js :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/07 21:16:09 by edbernar #+# #+# */
|
||||
/* Updated: 2024/08/07 21:18:22 by edbernar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
import { createNotification as NC } from "../notification/main.js";
|
||||
|
||||
function typeErrorInvalidPassword()
|
||||
{
|
||||
NC.new("Connection error", "Invalid mail or password", NC.defaultIcon.error);
|
||||
}
|
||||
|
||||
export { typeErrorInvalidPassword };
|
@ -6,20 +6,46 @@
|
||||
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/02 00:39:53 by edbernar #+# #+# */
|
||||
/* Updated: 2024/08/03 23:37:33 by edbernar ### ########.fr */
|
||||
/* Updated: 2024/08/07 22:14:49 by edbernar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
let userMeInfo = {
|
||||
username: "",
|
||||
id: 42
|
||||
id: -1
|
||||
};
|
||||
|
||||
let loginAvailable = false;
|
||||
let loginResolve = null;
|
||||
|
||||
function waitForLogin() {
|
||||
return new Promise((resolve) => {
|
||||
|
||||
if (loginAvailable)
|
||||
resolve();
|
||||
else
|
||||
loginResolve = resolve;
|
||||
});
|
||||
}
|
||||
|
||||
function typeLogin(content)
|
||||
{
|
||||
if (content != null)
|
||||
{
|
||||
console.log("Welcome " + content.username + "\nYou're id is " + content.id);
|
||||
userMeInfo.username = content.username;
|
||||
userMeInfo.id = content.id;
|
||||
}
|
||||
loginAvailable = true;
|
||||
if (loginResolve)
|
||||
{
|
||||
if (content != null)
|
||||
loginResolve(content.token);
|
||||
else
|
||||
loginResolve();
|
||||
loginResolve = null;
|
||||
loginAvailable = false;
|
||||
}
|
||||
}
|
||||
|
||||
export { userMeInfo, typeLogin };
|
||||
export { userMeInfo, typeLogin, waitForLogin };
|
@ -3,17 +3,18 @@
|
||||
/* ::: :::::::: */
|
||||
/* websocket.js :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ */
|
||||
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/31 22:17:24 by edbernar #+# #+# */
|
||||
/* Updated: 2024/08/07 19:00:30 by edbernar ### ########.fr */
|
||||
/* Updated: 2024/08/07 22:14:03 by edbernar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
import { typeLogin } from "./typeResponse/typeLogin.js";
|
||||
import { typePrivateListUser } from "./typeResponse/typePrivateListUser.js";
|
||||
import { typeErrorInvalidPassword } from "./typeErrorResponse/typeErrorInvalidPassword.js";
|
||||
import { typePrivateListMessage } from "./typeResponse/typePrivateListMessage.js";
|
||||
import { typeNewPrivateMessage } from "./typeResponse/typeNewPrivateMessage.js";
|
||||
import { typePrivateListUser } from "./typeResponse/typePrivateListUser.js";
|
||||
import { typeLogin } from "./typeResponse/typeLogin.js";
|
||||
|
||||
/*
|
||||
Todo (Eddy) :
|
||||
@ -27,11 +28,36 @@ const socket = new WebSocket('ws://localhost:8000/');
|
||||
const typeResponse = ["login", "private_list_user", "private_list_message", "new_private_message"];
|
||||
const functionResponse = [typeLogin, typePrivateListUser, typePrivateListMessage, typeNewPrivateMessage];
|
||||
|
||||
const errorCode = [9007]
|
||||
const errorFunction = [typeErrorInvalidPassword];
|
||||
|
||||
let status = 0;
|
||||
|
||||
function getCookie(name) {
|
||||
const value = `; ${document.cookie}`;
|
||||
const parts = value.split(`; ${name}=`);
|
||||
let token = null;
|
||||
|
||||
if (parts.length === 2)
|
||||
{
|
||||
token = parts.pop().split(';').shift();
|
||||
token = token.substring(1, token.length - 1);
|
||||
}
|
||||
return (token);
|
||||
}
|
||||
|
||||
socket.onopen = () => {
|
||||
let token = getCookie("token");
|
||||
|
||||
status = 1;
|
||||
console.log('Connected');
|
||||
if (token)
|
||||
{
|
||||
console.log("token :" + token);
|
||||
sendRequest("login", {type: "byToken", token: token});
|
||||
}
|
||||
else
|
||||
typeLogin(null);
|
||||
};
|
||||
|
||||
socket.onmessage = (event) => {
|
||||
@ -43,7 +69,13 @@ socket.onmessage = (event) => {
|
||||
return ;
|
||||
}
|
||||
if (response.code >= 9000 && response.code <= 9999)
|
||||
{
|
||||
try {
|
||||
errorFunction[errorCode.indexOf(response.code)]();
|
||||
} catch {
|
||||
console.warn(response);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
try {
|
||||
@ -68,6 +100,7 @@ function sendRequest(type, content) {
|
||||
coc = JSON.stringify(content);
|
||||
else
|
||||
coc = content;
|
||||
|
||||
socket.send(JSON.stringify({
|
||||
type: type,
|
||||
// token: token,
|
||||
|
@ -6,7 +6,7 @@
|
||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/08/03 15:54:14 by edbernar #+# #+# #
|
||||
# Updated: 2024/08/04 15:55:15 by edbernar ### ########.fr #
|
||||
# Updated: 2024/08/07 21:20:17 by edbernar ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -34,7 +34,6 @@ class User():
|
||||
print("\033[43m|------ User disconnected -------|\033[1;0m")
|
||||
print("User :", self.username)
|
||||
print("Id :", self.id)
|
||||
connected_clients.remove(self)
|
||||
|
||||
async def sendError(self, message, code, error=None):
|
||||
try:
|
||||
|
@ -6,7 +6,7 @@
|
||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/08/03 08:10:40 by edbernar #+# #+# #
|
||||
# Updated: 2024/08/04 14:31:26 by edbernar ### ########.fr #
|
||||
# Updated: 2024/08/07 21:22:18 by edbernar ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -14,7 +14,7 @@ from typeRequets.getPrivateListMessage import getPrivateListMessage
|
||||
from typeRequets.getPrivateListUser import getPrivateListUser
|
||||
from typeRequets.sendPrivateMessage import sendPrivateMessage
|
||||
from typeRequets.login import login
|
||||
from Class.User import User
|
||||
from Class.User import User, connected_clients
|
||||
import websockets
|
||||
import asyncio
|
||||
import json
|
||||
@ -54,6 +54,9 @@ async def handler(websocket, path):
|
||||
except websockets.ConnectionClosed:
|
||||
pass
|
||||
await userClass.close()
|
||||
connected_clients.remove(userClass)
|
||||
|
||||
|
||||
start_server = websockets.serve(handler, "localhost", 8000, subprotocols=['123456'])
|
||||
|
||||
asyncio.get_event_loop().run_until_complete(start_server)
|
||||
|
@ -6,7 +6,7 @@
|
||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/08/03 08:10:38 by edbernar #+# #+# #
|
||||
# Updated: 2024/08/04 15:51:04 by edbernar ### ########.fr #
|
||||
# Updated: 2024/08/07 22:21:58 by edbernar ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -28,18 +28,32 @@ userList = [
|
||||
"id": 9999999
|
||||
},
|
||||
{
|
||||
"username": "user2",
|
||||
"token": "789123",
|
||||
"mail": "bb@bb.fr",
|
||||
"password": "DEF456",
|
||||
"id": 2
|
||||
"username": "Eddy",
|
||||
"token": "54dsadw8f4a6w5f4a62s4f984fa62f4as65",
|
||||
"mail": "aaaaa",
|
||||
"password": "aaaaa",
|
||||
"id": 2135421
|
||||
},
|
||||
{
|
||||
"username": "user3",
|
||||
"token": "456789",
|
||||
"mail": "cc@cc,fr",
|
||||
"password": "GHI789",
|
||||
"id": 3
|
||||
"username": "Hugo",
|
||||
"token": "dsa4d6sa4sa1hfd1jhgk6g4k21bn65m4nb4",
|
||||
"mail": "bbbbb",
|
||||
"password": "bbbbb",
|
||||
"id": 9892154
|
||||
},
|
||||
{
|
||||
"username": "Mathis",
|
||||
"token": "8cb1qjlfndc12mn2l1mn654xzkkhad54cxz",
|
||||
"mail": "ccccc",
|
||||
"password": "ccccc",
|
||||
"id": 2371234
|
||||
},
|
||||
{
|
||||
"username": "Tom",
|
||||
"token": "poiuygfvbdsv5c21vcxvcxhgbjqnkmds546",
|
||||
"mail": "ddddd",
|
||||
"password": "ddddd",
|
||||
"id": 2371234
|
||||
}
|
||||
]
|
||||
|
||||
|
Reference in New Issue
Block a user