switch login method to async to work with the login request
This commit is contained in:
@ -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/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 json
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
@sync_to_async
|
||||||
def loginByPass(socket, content):
|
def loginByPass(socket, content):
|
||||||
password_hash = hashlib.md5((content["mail"] + content["password"]).encode()).hexdigest()
|
password_hash = hashlib.md5((content["mail"] + content["password"]).encode()).hexdigest()
|
||||||
user = User.objects.filter(mail=content["mail"], password=password_hash)
|
user = User.objects.filter(mail=content["mail"], password=password_hash)
|
||||||
@ -30,14 +31,15 @@ def loginByPass(socket, content):
|
|||||||
"username":user[0].username,
|
"username":user[0].username,
|
||||||
"id": user[0].id,
|
"id": user[0].id,
|
||||||
}}))
|
}}))
|
||||||
|
else:
|
||||||
|
socket.sendError("An unknown error occured",9027)
|
||||||
else:
|
else:
|
||||||
socket.sync_send(json.dumps({"type": "error", "content": "Invalid email or password", "code": 9007}))
|
socket.sync_send(json.dumps({"type": "error", "content": "Invalid email or password", "code": 9007}))
|
||||||
|
|
||||||
@sync_to_async
|
async def login(socket, content):
|
||||||
def login(socket, content):
|
|
||||||
try:
|
try:
|
||||||
if (content["type"] == "byPass"):
|
if (content["type"] == "byPass"):
|
||||||
loginByPass(socket, content)
|
await loginByPass(socket, content)
|
||||||
else:
|
else:
|
||||||
socket.sendError("Invalid login type", 9006)
|
socket.sendError("Invalid login type", 9006)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/09/09 14:31:30 by tomoron #+# #+# #
|
# 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)
|
print("\033[32monline : ", self.onlinePlayers)
|
||||||
return(0)
|
return(0)
|
||||||
|
|
||||||
def login(self, uid: int, username: str) -> int:
|
async def login(self, uid: int, username: str) -> int:
|
||||||
if(self.session_get("logged_in", False)):
|
if(await self.session_get("logged_in", False)):
|
||||||
|
print("already logged in")
|
||||||
return(0)
|
return(0)
|
||||||
if(not self.add_to_online(uid)):
|
if(not self.add_to_online(uid)):
|
||||||
socket.sendError("Already logged in", 9012)
|
self.sendError("Already logged in", 9012)
|
||||||
return(0)
|
return(0)
|
||||||
self.session_set("logged_in",True)
|
await self.session_set("logged_in",True)
|
||||||
self.session_set("id",uid)
|
await self.session_set("id",uid)
|
||||||
self.session_set("username",username)
|
await self.session_set("username",username)
|
||||||
self.session_save()
|
await self.session_save()
|
||||||
self.logged_in = True
|
self.logged_in = True
|
||||||
self.id = uid
|
self.id = uid
|
||||||
self.username = username
|
self.username = username
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
- 9024 : An error occured while creating the account
|
- 9024 : An error occured while creating the account
|
||||||
- 9025 : Account not verified, please verify your account before logging in
|
- 9025 : Account not verified, please verify your account before logging in
|
||||||
- 9026 : An error occured while sending the email, glhf
|
- 9026 : An error occured while sending the email, glhf
|
||||||
|
- 9027 : An unknown error occured
|
||||||
|
|
||||||
- 9100 : Action out of range
|
- 9100 : Action out of range
|
||||||
- 9101 : No game started
|
- 9101 : No game started
|
||||||
|
Reference in New Issue
Block a user