PATCH: fixed issues with authentication

This commit is contained in:
white
2025-01-29 11:31:43 +00:00
parent cd1a246483
commit e2070be9f9
9 changed files with 86 additions and 107 deletions

View File

@@ -26,7 +26,7 @@ def signup():
# Validation - ensure all fields exist, users cannot have an empty field
if not all([username, email, password]):
error_fields = get_error_fields([username, email, password]), #!←← find the error_fields, to highlight them in red to the user on the frontend
error_fields = get_error_fields([username, email, password]) #!←← find the error_fields, to highlight them in red to the user on the frontend
return jsonify({
"account_created": False,
"error_fields": error_fields,
@@ -48,25 +48,25 @@ def signup():
# Create a connection to the database
db = Database()
cursor = db.create_connection()
db.create_connection()
try:
# Check for duplicate email/username, no two users can have the same
dup_email = cursor.execute(
dup_email = db.fetchone(
"SELECT * FROM users WHERE email = ?",
(email,)
).fetchone()
)
dup_username = cursor.execute(
dup_username = db.fetchone(
"SELECT * FROM users WHERE username = ?",
(username,)
).fetchone()
)
if dup_email is not None:
return jsonify({
"account_created": False,
"error_fields": ["email"],
"message": "Email already taken"
"message": f"Email already taken: {email}"
}), 400
if dup_username is not None:
@@ -77,7 +77,7 @@ def signup():
}), 400
# Create new user once input is validated
cursor.execute(
db.execute(
"""INSERT INTO users
(username, password, email, num_followers, stream_key, is_partnered, bio, current_stream_title, current_selected_category_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)""",
@@ -151,14 +151,14 @@ def login():
# Create a connection to the database
db = Database()
cursor = db.create_connection()
db.create_connection()
try:
# Check if user exists, only existing users can be logged in
user = cursor.execute(
user = db.fetchone(
"SELECT * FROM users WHERE username = ?",
(username,)
).fetchone()
)
if not user:
return jsonify({
@@ -210,7 +210,4 @@ def logout() -> dict:
def get_error_fields(values: list):
fields = ["username", "email", "password"]
for x in fields:
if not values[fields.index(x)]:
fields.remove(x)
return fields
return [fields[i] for i, v in enumerate(values) if not v]

View File

@@ -51,10 +51,10 @@ def get_past_chat(stream_id: int):
# Connect to the database
db = Database()
cursor = db.create_connection()
db.create_connection()
# fetched in format: [(chatter_id, message, time_sent)]
all_chats = cursor.execute("""
all_chats = db.fetchall("""
SELECT *
FROM (
SELECT chatter_id, message, time_sent
@@ -63,7 +63,7 @@ def get_past_chat(stream_id: int):
ORDER BY time_sent DESC
LIMIT 50
)
ORDER BY time_sent ASC;""", (stream_id,)).fetchall()
ORDER BY time_sent ASC;""", (stream_id,))
db.close_connection()
# Create JSON output of chat_history to pass through NGINX proxy
@@ -103,8 +103,8 @@ def send_chat(data) -> None:
def save_chat(chatter_id, stream_id, message):
"""Save the chat to the database"""
db = Database()
cursor = db.create_connection()
cursor.execute("""
db.create_connection()
db.execute("""
INSERT INTO chat (chatter_id, stream_id, message)
VALUES (?, ?, ?);""", (chatter_id, stream_id, message))
db.commit_data()

View File

@@ -34,7 +34,7 @@ def get_recommended_streams() -> list[dict]:
Queries DB to get a list of recommended streams using an algorithm
"""
user_id = session.get("user_id")
user_id = session.get("username")
category = user_recommendation_category(user_id)
streams = recommendations_based_on_category(category)
return jsonify(streams)
@@ -180,7 +180,7 @@ def publish_stream():
1,
datetime.now(),
1))
db.commit_data()
return redirect(f"/{user_info['username']}")
@@ -198,5 +198,6 @@ def end_stream():
# Set stream to not live
db.execute("""UPDATE streams SET isLive = 0 WHERE user_id = ? AND isLive = 1""", (user_info["user_id"],))
db.commit_data()
return "Stream ended", 200