switch login method to async to work with the login request

This commit is contained in:
2024-09-15 00:49:44 +02:00
parent 55cc50995d
commit 98ddde7233
3 changed files with 16 additions and 12 deletions

View File

@ -6,7 +6,7 @@
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/08/03 08:10:38 by edbernar #+# #+# #
# Updated: 2024/09/14 18:54:47 by tomoron ### ########.fr #
# Updated: 2024/09/15 00:47:18 by tomoron ### ########.fr #
# #
# **************************************************************************** #
@ -17,6 +17,7 @@ import requests
import json
import os
@sync_to_async
def loginByPass(socket, content):
password_hash = hashlib.md5((content["mail"] + content["password"]).encode()).hexdigest()
user = User.objects.filter(mail=content["mail"], password=password_hash)
@ -30,14 +31,15 @@ def loginByPass(socket, content):
"username":user[0].username,
"id": user[0].id,
}}))
else:
socket.sendError("An unknown error occured",9027)
else:
socket.sync_send(json.dumps({"type": "error", "content": "Invalid email or password", "code": 9007}))
@sync_to_async
def login(socket, content):
async def login(socket, content):
try:
if (content["type"] == "byPass"):
loginByPass(socket, content)
await loginByPass(socket, content)
else:
socket.sendError("Invalid login type", 9006)
except Exception as e:

View File

@ -6,7 +6,7 @@
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/09 14:31:30 by tomoron #+# #+# #
# Updated: 2024/09/14 21:21:19 by tomoron ### ########.fr #
# Updated: 2024/09/15 00:48:29 by tomoron ### ########.fr #
# #
# **************************************************************************** #
@ -61,16 +61,17 @@ class WebsocketHandler(AsyncWebsocketConsumer):
print("\033[32monline : ", self.onlinePlayers)
return(0)
def login(self, uid: int, username: str) -> int:
if(self.session_get("logged_in", False)):
async def login(self, uid: int, username: str) -> int:
if(await self.session_get("logged_in", False)):
print("already logged in")
return(0)
if(not self.add_to_online(uid)):
socket.sendError("Already logged in", 9012)
self.sendError("Already logged in", 9012)
return(0)
self.session_set("logged_in",True)
self.session_set("id",uid)
self.session_set("username",username)
self.session_save()
await self.session_set("logged_in",True)
await self.session_set("id",uid)
await self.session_set("username",username)
await self.session_save()
self.logged_in = True
self.id = uid
self.username = username