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 :+: :+: :+: */
|
/* createConnectDiv.js :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ */
|
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/08/07 18:14:53 by edbernar #+# #+# */
|
/* 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";
|
import { sendRequest } from "../websocket.js";
|
||||||
|
|
||||||
function createConnectDiv()
|
function createConnectDiv()
|
||||||
@ -43,10 +44,21 @@ function addGlobalBg()
|
|||||||
|
|
||||||
function createButton(inputLogin, inputPass)
|
function createButton(inputLogin, inputPass)
|
||||||
{
|
{
|
||||||
|
const loginButton = document.getElementById('loginButton');
|
||||||
|
const pLoginButton = loginButton.getElementsByTagName('p')[0];
|
||||||
const button = document.createElement("button");
|
const button = document.createElement("button");
|
||||||
|
let usernameNode = null;
|
||||||
|
|
||||||
button.addEventListener('click', () => {
|
button.addEventListener('click', () => {
|
||||||
sendRequest("login", {type: "byPass", mail: inputLogin.value, password: inputPass.value});
|
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);
|
return (button);
|
||||||
}
|
}
|
||||||
|
@ -3,22 +3,32 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* main.js :+: :+: :+: */
|
/* main.js :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ */
|
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/08/07 17:40:15 by edbernar #+# #+# */
|
/* 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 { createNotification as CN } from "../notification/main.js";
|
||||||
|
import { userMeInfo, waitForLogin } from "../typeResponse/typeLogin.js";
|
||||||
import { createConnectDiv } from "./createConnectDiv.js";
|
import { createConnectDiv } from "./createConnectDiv.js";
|
||||||
import { createThreeDiv } from "./createThreeDiv.js";
|
import { createThreeDiv } from "./createThreeDiv.js";
|
||||||
|
|
||||||
function login()
|
function login()
|
||||||
{
|
{
|
||||||
const loginButton = document.getElementById('loginButton');
|
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);
|
loginButton.addEventListener('click', showLoginDiv);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function 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. +#+ +:+ +#+ */
|
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/08/02 00:39:53 by edbernar #+# #+# */
|
/* 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 = {
|
let userMeInfo = {
|
||||||
username: "",
|
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)
|
function typeLogin(content)
|
||||||
|
{
|
||||||
|
if (content != null)
|
||||||
{
|
{
|
||||||
console.log("Welcome " + content.username + "\nYou're id is " + content.id);
|
console.log("Welcome " + content.username + "\nYou're id is " + content.id);
|
||||||
userMeInfo.username = content.username;
|
userMeInfo.username = content.username;
|
||||||
userMeInfo.id = content.id;
|
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 :+: :+: :+: */
|
/* websocket.js :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ */
|
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/07/31 22:17:24 by edbernar #+# #+# */
|
/* 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 { typeErrorInvalidPassword } from "./typeErrorResponse/typeErrorInvalidPassword.js";
|
||||||
import { typePrivateListUser } from "./typeResponse/typePrivateListUser.js";
|
|
||||||
import { typePrivateListMessage } from "./typeResponse/typePrivateListMessage.js";
|
import { typePrivateListMessage } from "./typeResponse/typePrivateListMessage.js";
|
||||||
import { typeNewPrivateMessage } from "./typeResponse/typeNewPrivateMessage.js";
|
import { typeNewPrivateMessage } from "./typeResponse/typeNewPrivateMessage.js";
|
||||||
|
import { typePrivateListUser } from "./typeResponse/typePrivateListUser.js";
|
||||||
|
import { typeLogin } from "./typeResponse/typeLogin.js";
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Todo (Eddy) :
|
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 typeResponse = ["login", "private_list_user", "private_list_message", "new_private_message"];
|
||||||
const functionResponse = [typeLogin, typePrivateListUser, typePrivateListMessage, typeNewPrivateMessage];
|
const functionResponse = [typeLogin, typePrivateListUser, typePrivateListMessage, typeNewPrivateMessage];
|
||||||
|
|
||||||
|
const errorCode = [9007]
|
||||||
|
const errorFunction = [typeErrorInvalidPassword];
|
||||||
|
|
||||||
let status = 0;
|
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 = () => {
|
socket.onopen = () => {
|
||||||
|
let token = getCookie("token");
|
||||||
|
|
||||||
status = 1;
|
status = 1;
|
||||||
console.log('Connected');
|
console.log('Connected');
|
||||||
|
if (token)
|
||||||
|
{
|
||||||
|
console.log("token :" + token);
|
||||||
|
sendRequest("login", {type: "byToken", token: token});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
typeLogin(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
socket.onmessage = (event) => {
|
socket.onmessage = (event) => {
|
||||||
@ -43,7 +69,13 @@ socket.onmessage = (event) => {
|
|||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
if (response.code >= 9000 && response.code <= 9999)
|
if (response.code >= 9000 && response.code <= 9999)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
errorFunction[errorCode.indexOf(response.code)]();
|
||||||
|
} catch {
|
||||||
console.warn(response);
|
console.warn(response);
|
||||||
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@ -68,6 +100,7 @@ function sendRequest(type, content) {
|
|||||||
coc = JSON.stringify(content);
|
coc = JSON.stringify(content);
|
||||||
else
|
else
|
||||||
coc = content;
|
coc = content;
|
||||||
|
|
||||||
socket.send(JSON.stringify({
|
socket.send(JSON.stringify({
|
||||||
type: type,
|
type: type,
|
||||||
// token: token,
|
// token: token,
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/08/03 15:54:14 by edbernar #+# #+# #
|
# 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("\033[43m|------ User disconnected -------|\033[1;0m")
|
||||||
print("User :", self.username)
|
print("User :", self.username)
|
||||||
print("Id :", self.id)
|
print("Id :", self.id)
|
||||||
connected_clients.remove(self)
|
|
||||||
|
|
||||||
async def sendError(self, message, code, error=None):
|
async def sendError(self, message, code, error=None):
|
||||||
try:
|
try:
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/08/03 08:10:40 by edbernar #+# #+# #
|
# 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.getPrivateListUser import getPrivateListUser
|
||||||
from typeRequets.sendPrivateMessage import sendPrivateMessage
|
from typeRequets.sendPrivateMessage import sendPrivateMessage
|
||||||
from typeRequets.login import login
|
from typeRequets.login import login
|
||||||
from Class.User import User
|
from Class.User import User, connected_clients
|
||||||
import websockets
|
import websockets
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
@ -54,6 +54,9 @@ async def handler(websocket, path):
|
|||||||
except websockets.ConnectionClosed:
|
except websockets.ConnectionClosed:
|
||||||
pass
|
pass
|
||||||
await userClass.close()
|
await userClass.close()
|
||||||
|
connected_clients.remove(userClass)
|
||||||
|
|
||||||
|
|
||||||
start_server = websockets.serve(handler, "localhost", 8000, subprotocols=['123456'])
|
start_server = websockets.serve(handler, "localhost", 8000, subprotocols=['123456'])
|
||||||
|
|
||||||
asyncio.get_event_loop().run_until_complete(start_server)
|
asyncio.get_event_loop().run_until_complete(start_server)
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/08/03 08:10:38 by edbernar #+# #+# #
|
# 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
|
"id": 9999999
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"username": "user2",
|
"username": "Eddy",
|
||||||
"token": "789123",
|
"token": "54dsadw8f4a6w5f4a62s4f984fa62f4as65",
|
||||||
"mail": "bb@bb.fr",
|
"mail": "aaaaa",
|
||||||
"password": "DEF456",
|
"password": "aaaaa",
|
||||||
"id": 2
|
"id": 2135421
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"username": "user3",
|
"username": "Hugo",
|
||||||
"token": "456789",
|
"token": "dsa4d6sa4sa1hfd1jhgk6g4k21bn65m4nb4",
|
||||||
"mail": "cc@cc,fr",
|
"mail": "bbbbb",
|
||||||
"password": "GHI789",
|
"password": "bbbbb",
|
||||||
"id": 3
|
"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