Rolled back login endpoint functionality, added endpoints for managing friend requests.
This commit is contained in:
parent
da53dbe060
commit
3e6ddd9d54
52
calls.py
52
calls.py
|
@ -3,6 +3,7 @@ from db import get_db_connection
|
|||
import base64
|
||||
import secrets
|
||||
import time
|
||||
import json
|
||||
|
||||
def register_user(email, display_name, password):
|
||||
hashed_password = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
|
||||
|
@ -34,10 +35,9 @@ def authenticate_user(email, password):
|
|||
random_int = secrets.randbelow(1000000)
|
||||
token_data = f"{email}:{password}:{epoch_timestamp}:{random_int}"
|
||||
encoded_token = base64.b64encode(token_data.encode()).decode()
|
||||
hashed_token = bcrypt.hashpw(encoded_token.encode(), bcrypt.gensalt())
|
||||
cursor.execute("UPDATE users SET session_token = %s WHERE email = %s;", (hashed_token, email))
|
||||
cursor.execute("UPDATE users SET session_token = %s WHERE email = %s;", (encoded_token, email))
|
||||
conn.commit()
|
||||
return hashed_token.decode()
|
||||
return encoded_token
|
||||
return None
|
||||
|
||||
finally:
|
||||
|
@ -49,7 +49,7 @@ def reauth_user(token):
|
|||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute("SELECT email, display_name, current_elo, session_token FROM users WHERE session_token IS = %s;", (token,))
|
||||
cursor.execute("SELECT email, display_name, current_elo, session_token FROM users WHERE session_token = %s;", (token,))
|
||||
user = cursor.fetchone()
|
||||
if user:
|
||||
user_data = {
|
||||
|
@ -57,25 +57,38 @@ def reauth_user(token):
|
|||
"display_name": user["display_name"],
|
||||
"elo": user["current_elo"]
|
||||
}
|
||||
return json.dumps(user_data)
|
||||
return user_data
|
||||
return None
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
def add_friend(user_uid, friend_uid):
|
||||
def add_friend(token, friend_uid):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute("SELECT friend_list FROM users WHERE uid = %s;", (user_uid,))
|
||||
friends = cursor.fetchone()["friend_list"]
|
||||
if friend_uid not in friends:
|
||||
friends.append(friend_uid)
|
||||
cursor.execute("SELECT friend_list FROM users WHERE session_token = %s;", (token,))
|
||||
result = cursor.fetchone()
|
||||
if result:
|
||||
friends = result["friend_list"] or {}
|
||||
else:
|
||||
friends = {}
|
||||
|
||||
index = len(friends)
|
||||
|
||||
friend_key = f"friend{index}"
|
||||
if friend_key not in friends:
|
||||
friends[friend_key] = friend_uid
|
||||
friends_json = json.dumps(friends)
|
||||
|
||||
cursor.execute(
|
||||
"UPDATE users SET friend_list = %s WHERE uid = %s;",
|
||||
(friends, user_uid)
|
||||
"UPDATE users SET friend_list = %s WHERE session_token = %s;",
|
||||
(friends_json, token)
|
||||
)
|
||||
conn.commit()
|
||||
else:
|
||||
return False
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
conn.rollback()
|
||||
|
@ -84,6 +97,20 @@ def add_friend(user_uid, friend_uid):
|
|||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
|
||||
def get_leaderboard():
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute("SELECT display_name, current_elo, uid FROM users WHERE current_elo IS NOT NULL ORDER BY current_elo DESC;")
|
||||
players = cursor.fetchall()
|
||||
player_elo_list = [{"player_name": player["display_name"], "elo_rating": player["current_elo"], "friend_code": player["uid"]} for player in players]
|
||||
return player_elo_list
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
|
||||
def send_match_invite(sender_uid, receiver_uid):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
@ -252,3 +279,4 @@ def update_elo(player1_display_name, player2_display_name, player1_score, player
|
|||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
|
|
68
main.py
68
main.py
|
@ -1,7 +1,8 @@
|
|||
from fastapi import FastAPI, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from calls import register_user, authenticate_user, reauth_user, add_friend, send_match_invite, accept_match_invite, get_all_matches, get_elo, update_elo
|
||||
from calls import *
|
||||
from db import get_db_connection
|
||||
|
||||
app = FastAPI()
|
||||
app.add_middleware(
|
||||
|
@ -20,8 +21,11 @@ class LoginRequest(BaseModel):
|
|||
email: str
|
||||
password: str
|
||||
|
||||
class ReloginRequest(BaseModel):
|
||||
token: str
|
||||
|
||||
class FriendRequest(BaseModel):
|
||||
user_uid: int
|
||||
token: str
|
||||
friend_uid: int
|
||||
|
||||
class MatchInviteRequest(BaseModel):
|
||||
|
@ -32,6 +36,9 @@ class AcceptInviteRequest(BaseModel):
|
|||
match_id: int
|
||||
player2_uid: int
|
||||
|
||||
class getFriendList(BaseModel):
|
||||
token: str
|
||||
|
||||
@app.post("/register")
|
||||
def register(request: RegisterRequest):
|
||||
try:
|
||||
|
@ -48,10 +55,18 @@ def login(request: LoginRequest):
|
|||
else:
|
||||
raise HTTPException(status_code=401, detail="Invalid credentials")
|
||||
|
||||
@app.post("/auth")
|
||||
def login(request: ReloginRequest):
|
||||
sessiontoken = reauth_user(request.token)
|
||||
if sessiontoken:
|
||||
return {"message": "Login successful", "uid": sessiontoken}
|
||||
else:
|
||||
raise HTTPException(status_code=503, detail="Bad Token")
|
||||
|
||||
@app.post("/add_friend")
|
||||
def add_friend_endpoint(request: FriendRequest):
|
||||
try:
|
||||
success = add_friend(request.user_uid, request.friend_uid)
|
||||
success = add_friend(request.token, request.friend_uid)
|
||||
return {"message": "Friend added successfully"} if success else HTTPException(400, "Failed to add friend")
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
@ -80,6 +95,14 @@ def get_matches():
|
|||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
@app.get("/leaderboards")
|
||||
def leaderboard():
|
||||
try:
|
||||
leaderboard = get_leaderboard()
|
||||
return leaderboard
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
@app.post("/elo")
|
||||
def get_elo_endpoint(authorization: str):
|
||||
print(f"Received Authorization header: {authorization}")
|
||||
|
@ -88,3 +111,42 @@ def get_elo_endpoint(authorization: str):
|
|||
return result
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
def get_friend_details(friend_uid):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute("SELECT uid, display_name FROM users WHERE uid = %s;", (friend_uid,))
|
||||
result = cursor.fetchone()
|
||||
if result:
|
||||
return {"uid": result["uid"], "name": result["display_name"]}
|
||||
return None
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
@app.post("/get_friends")
|
||||
def get_friends_list(request: getFriendList):
|
||||
token = request.token
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
|
||||
cursor.execute("SELECT friend_list FROM users WHERE session_token = %s;", (token,))
|
||||
result = cursor.fetchone()
|
||||
|
||||
if not result or not result["friend_list"]:
|
||||
raise HTTPException(status_code=404, detail="No friends found.")
|
||||
friends = result["friend_list"]
|
||||
friends_details = []
|
||||
for key, friend_uid in friends.items():
|
||||
friend_details = get_friend_details(friend_uid)
|
||||
if friend_details:
|
||||
friends_details.append(friend_details)
|
||||
|
||||
return {"friends": friends_details}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
Loading…
Reference in a new issue