Patch: Tidy up of style code and fix to authentication logic
Feat: Added ability to access user's username through AuthContext
This commit is contained in:
@@ -25,13 +25,10 @@ def signup():
|
||||
|
||||
# Validation - ensure all fields exist, users cannot have an empty field
|
||||
if not all([username, email, password]):
|
||||
fields = ["username", "email", "password"]
|
||||
for x in fields:
|
||||
if not [username, email, password][fields.index(x)]:
|
||||
fields.remove(x)
|
||||
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": fields,
|
||||
"error_fields": error_fields,
|
||||
"message": "Missing required fields"
|
||||
}), 400
|
||||
|
||||
@@ -41,9 +38,10 @@ def signup():
|
||||
email = sanitizer(email, "email")
|
||||
password = sanitizer(password, "password")
|
||||
except ValueError as e:
|
||||
error_fields = get_error_fields([username, email, password])
|
||||
return jsonify({
|
||||
"account_created": False,
|
||||
"error_fields": fields,
|
||||
"error_fields": error_fields,
|
||||
"message": "Invalid input received"
|
||||
}), 400
|
||||
|
||||
@@ -204,3 +202,10 @@ def logout() -> dict:
|
||||
"""
|
||||
session.clear()
|
||||
return {"logged_in": False}
|
||||
|
||||
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
|
||||
@@ -9,6 +9,7 @@ socketio = SocketIO()
|
||||
# <---------------------- ROUTES NEEDS TO BE CHANGED TO VIDEO OR DELETED AS DEEMED APPROPRIATE ---------------------->
|
||||
# TODO: Add a route that deletes all chat logs when the stream is finished
|
||||
|
||||
|
||||
@socketio.on("connect")
|
||||
def handle_connection() -> None:
|
||||
"""
|
||||
@@ -16,6 +17,7 @@ def handle_connection() -> None:
|
||||
"""
|
||||
print("Client Connected") # Confirmation connect has been made
|
||||
|
||||
|
||||
@socketio.on("join")
|
||||
def handle_join(data) -> None:
|
||||
"""
|
||||
@@ -26,6 +28,7 @@ def handle_join(data) -> None:
|
||||
join_room(stream_id)
|
||||
emit("status", {"message": f"Welcome to the chat, stream_id: {stream_id}"}, room=stream_id)
|
||||
|
||||
|
||||
@socketio.on("leave")
|
||||
def handle_leave(data) -> None:
|
||||
"""
|
||||
@@ -36,11 +39,12 @@ def handle_leave(data) -> None:
|
||||
leave_room(stream_id)
|
||||
emit("status", {"message": f"user left room {stream_id}"}, room=stream_id)
|
||||
|
||||
|
||||
@chat_bp.route("/chat/<int:stream_id>")
|
||||
def get_past_chat(stream_id: int):
|
||||
"""
|
||||
Returns a JSON object to be passed to the server.
|
||||
|
||||
|
||||
Output structure in the following format: `{chatter_id: message}` for all chats.
|
||||
|
||||
Ran once when a user first logs into a stream to get the most recent 50 chat messages.
|
||||
@@ -49,7 +53,7 @@ def get_past_chat(stream_id: int):
|
||||
# Connect to the database
|
||||
db = Database()
|
||||
cursor = db.create_connection()
|
||||
|
||||
|
||||
# fetched in format: [(chatter_id, message, time_sent)]
|
||||
all_chats = cursor.execute("""
|
||||
SELECT *
|
||||
@@ -62,13 +66,15 @@ def get_past_chat(stream_id: int):
|
||||
)
|
||||
ORDER BY time_sent ASC;""", (stream_id,)).fetchall()
|
||||
db.close_connection()
|
||||
|
||||
|
||||
# Create JSON output of chat_history to pass through NGINX proxy
|
||||
chat_history = [{"chatter_id": chat[0], "message": chat[1], "time_sent": chat[2]} for chat in all_chats]
|
||||
chat_history = [{"chatter_id": chat[0], "message": chat[1],
|
||||
"time_sent": chat[2]} for chat in all_chats]
|
||||
|
||||
# Pass the chat history to the proxy
|
||||
return jsonify({"chat_history": chat_history}), 200
|
||||
|
||||
|
||||
@socketio.on("send_message")
|
||||
def send_chat(data) -> None:
|
||||
"""
|
||||
@@ -84,7 +90,7 @@ def send_chat(data) -> None:
|
||||
if not all([chatter_id, message, stream_id]):
|
||||
emit("error", {"error": "Unable to send a chat"}, broadcast=False)
|
||||
return
|
||||
|
||||
|
||||
# Save chat information to database so other users can see
|
||||
db = Database()
|
||||
cursor = db.create_connection()
|
||||
@@ -96,7 +102,7 @@ def send_chat(data) -> None:
|
||||
|
||||
# Send the chat message to the client so it can be displayed
|
||||
emit("new_message", {
|
||||
"chatter_id":chatter_id,
|
||||
"message":message,
|
||||
"chatter_id": chatter_id,
|
||||
"message": message,
|
||||
"time_sent": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
}, room=stream_id)
|
||||
}, room=stream_id)
|
||||
|
||||
@@ -3,6 +3,7 @@ from utils.user_utils import is_subscribed, is_following, subscription_expiratio
|
||||
|
||||
user_bp = Blueprint("user", __name__)
|
||||
|
||||
|
||||
@user_bp.route('/is_subscribed/<int:user_id>/<int:streamer_id>')
|
||||
def user_subscribed(user_id: int, streamer_id: int):
|
||||
"""
|
||||
@@ -19,7 +20,7 @@ def user_following(user_id: int, streamer_id: int):
|
||||
"""
|
||||
if is_following(user_id, streamer_id):
|
||||
return jsonify({"following": True})
|
||||
return jsonify({"following": False})
|
||||
return jsonify({"following": False})
|
||||
|
||||
|
||||
@user_bp.route('/subscription_remaining/<int:user_id>/<int:streamer_id>')
|
||||
@@ -28,7 +29,7 @@ def user_subscription_expiration(user_id: int, streamer_id: int):
|
||||
Returns remaining time until subscription expiration
|
||||
"""
|
||||
remaining_time = subscription_expiration(user_id, streamer_id)
|
||||
|
||||
|
||||
return jsonify({"remaining_time": remaining_time})
|
||||
|
||||
@user_bp.route('/get_login_status')
|
||||
@@ -36,7 +37,9 @@ def get_login_status():
|
||||
"""
|
||||
Returns whether the user is logged in or not
|
||||
"""
|
||||
return jsonify(session.get("username") is not None)
|
||||
username = session.get("username")
|
||||
return jsonify({'status': username is not None, 'username': username})
|
||||
|
||||
|
||||
@user_bp.route('/authenticate_user')
|
||||
def authenticate_user() -> dict:
|
||||
@@ -45,6 +48,7 @@ def authenticate_user() -> dict:
|
||||
"""
|
||||
return {"authenticated": True}
|
||||
|
||||
|
||||
@user_bp.route('/forgot_password', methods=['POST'])
|
||||
def forgot_password():
|
||||
"""
|
||||
|
||||
@@ -8,6 +8,7 @@ Flask==3.1.0
|
||||
Flask-Session==0.8.0
|
||||
Flask-WTF==1.2.2
|
||||
Flask_CORS==5.0.0
|
||||
flask-socketio==5.5.1
|
||||
python-dotenv==1.0.1
|
||||
idna==3.10
|
||||
itsdangerous==2.2.0
|
||||
@@ -20,5 +21,4 @@ typing_extensions==4.12.2
|
||||
urllib3==2.3.0
|
||||
Werkzeug==3.1.3
|
||||
WTForms==3.2.1
|
||||
Gunicorn==20.1.0
|
||||
flask-socketio==5.5.1
|
||||
Gunicorn==20.1.0
|
||||
Reference in New Issue
Block a user