diff --git a/TFR/server/__init__.py b/TFR/server/__init__.py index 1f42466..077f7e4 100644 --- a/TFR/server/__init__.py +++ b/TFR/server/__init__.py @@ -4,10 +4,10 @@ from flask import Flask, render_template, abort from flask_assets import Bundle from werkzeug.exceptions import HTTPException -from server.extensions import db, migrate, cache, assets, login_manager -from server.models import Users -from server.config import MIGRATION_DIR, INSTANCE_DIR -from server import views, auth, api, filters +from .config import MIGRATION_DIR, INSTANCE_DIR +from .extensions import db, migrate, cache, assets, login_manager +from .models import Users +from . import views, auth, api, filters app = Flask(__name__, instance_path=INSTANCE_DIR) diff --git a/TFR/server/api.py b/TFR/server/api.py index 171a4c2..b873962 100644 --- a/TFR/server/api.py +++ b/TFR/server/api.py @@ -5,13 +5,10 @@ from flask import Blueprint, request, jsonify from flask_login import login_required, current_user from werkzeug.security import check_password_hash -from server.models import Scores, Sessions, Users -from server.extensions import db -from server.config import ( - GAME_VERSION, - GAME_VERSIONS, +from .models import Scores, Sessions, Users +from .extensions import db +from .config import ( GAME_DIFFICULTIES, - USER_MAX_TOKENS, MAX_SEARCH_RESULTS, USER_REGEX, ) @@ -82,7 +79,7 @@ def post(): @blueprint.route("/search", methods=["GET"]) def search(): - search_arg = request.args.get("q") + search_arg = request.args.get("q").strip() if not search_arg: return "No search query provided!", 400 @@ -98,16 +95,15 @@ def search(): @blueprint.route("/login", methods=["POST"]) def login(): - username = request.form["username"].strip() - password = request.form["password"].strip() - device = request.form["device"].strip() + username = request.form.get("username", None).strip() + password = request.form.get("password", None).strip() + device = request.form.get("device", "Unknown").strip() username_regex = re.compile(USER_REGEX) if not username or not username_regex.match(username) or not password: return "Username or Password is incorrect!", 400 user = Users.query.filter_by(username=username).first() - if not user or not check_password_hash(user.password, password): return "Username or Password is incorrect!", 400 @@ -125,10 +121,9 @@ def login(): @blueprint.route("/authenticate", methods=["POST"]) def authenticate(): - auth_key = request.form["auth_key"].strip() + auth_key = request.form.get("session", None).strip() session = Sessions.query.filter_by(auth_key=auth_key).first() - if not session: return "Invalid session", 400 diff --git a/TFR/server/auth.py b/TFR/server/auth.py index 9132d7f..607d126 100644 --- a/TFR/server/auth.py +++ b/TFR/server/auth.py @@ -2,12 +2,12 @@ import re import uuid from flask import Blueprint, render_template, request, flash, redirect, url_for -from flask_login import login_required, login_user, logout_user, current_user +from flask_login import login_user from werkzeug.security import generate_password_hash, check_password_hash -from server.extensions import db -from server.models import Users, Sessions -from server.config import USER_REGEX, USER_EMAIL_REGEX +from .extensions import db +from .models import Users +from .config import USER_REGEX blueprint = Blueprint("auth", __name__) @@ -21,23 +21,20 @@ def auth(): @blueprint.route("/register", methods=["POST"]) def register(): # Get the form data - username = request.form["username"].strip() - email = request.form["email"].strip() - password = request.form["password"].strip() + username = request.form.get("username", None).strip() + password = request.form.get("password", None).strip() + confirm = request.form.get("confirm", None).strip() username_regex = re.compile(USER_REGEX) - email_regex = re.compile(USER_EMAIL_REGEX) error = [] # Validate the form if not username or not username_regex.match(username): error.append("Username is invalid! Must be alphanumeric, and can contain ._-") - if not email or not email_regex.match(email): - error.append("Email is invalid! Must be email format") - if not password: - error.append("Password is empty!") - elif len(password) < 8: + if not password or len(password) < 8: error.append("Password is too short! Must be at least 8 characters long.") + if not confirm or password != confirm: + error.append("Passwords do not match!") if Users.query.filter_by(username=username).first(): error.append("Username already exists!") @@ -50,7 +47,6 @@ def register(): register_user = Users( alt_id=str(uuid.uuid4()), username=username, - email=generate_password_hash(email, method="scrypt"), password=generate_password_hash(password, method="scrypt"), ) db.session.add(register_user) diff --git a/TFR/server/config.py b/TFR/server/config.py index 5d109ae..9a6a266 100644 --- a/TFR/server/config.py +++ b/TFR/server/config.py @@ -5,7 +5,6 @@ GAME_VERSION = "alpha" GAME_VERSIONS = ["alpha"] GAME_DIFFICULTIES = [0, 1, 2, 3, 4] -USER_MAX_TOKENS = 3 USER_REGEX = r"\b[A-Za-z0-9._-]+\b" USER_EMAIL_REGEX = r"[^@]+@[^@]+\.[^@]+" diff --git a/TFR/server/models.py b/TFR/server/models.py index 9a32e8a..2c15299 100644 --- a/TFR/server/models.py +++ b/TFR/server/models.py @@ -1,10 +1,9 @@ """ Database models for the server """ -import uuid from flask_login import UserMixin -from server.extensions import db -from server.config import GAME_VERSION +from .extensions import db +from .config import GAME_VERSION class Scores(db.Model): @@ -115,7 +114,7 @@ class Users(db.Model, UserMixin): alt_id = db.Column(db.String, nullable=False, unique=True) username = db.Column(db.String(32), unique=True, nullable=False) - email = db.Column(db.String, unique=True, nullable=False) + email = db.Column(db.String) password = db.Column(db.String, nullable=False) joined_at = db.Column( diff --git a/TFR/server/templates/auth.html b/TFR/server/templates/auth.html index 148966a..e18379a 100644 --- a/TFR/server/templates/auth.html +++ b/TFR/server/templates/auth.html @@ -28,13 +28,13 @@ - - + + - - + + diff --git a/TFR/server/templates/base.html b/TFR/server/templates/base.html index 51bcdc2..79d0be4 100644 --- a/TFR/server/templates/base.html +++ b/TFR/server/templates/base.html @@ -50,7 +50,10 @@ {% if current_user.is_authenticated %} - {{ current_user.username }} + + {{ current_user.username }} + {% if not current_user.email %}{% endif %} + {% else %} {% endif %} diff --git a/TFR/server/templates/settings.html b/TFR/server/templates/settings.html index 6d6e72e..bc08f03 100644 --- a/TFR/server/templates/settings.html +++ b/TFR/server/templates/settings.html @@ -1,5 +1,12 @@ {% extends "base.html" %} {% block content %} + {% if not current_user.email %} +
+

No Email set

+

If you forget your password, you will not be able to recover your account.

+
+ {% endif %} +

Hello, {{ current_user.username }}!

Sample text

diff --git a/TFR/server/views.py b/TFR/server/views.py index 65fb89a..3cdc99c 100644 --- a/TFR/server/views.py +++ b/TFR/server/views.py @@ -1,7 +1,7 @@ from flask import Blueprint, request, render_template, abort, flash, redirect, url_for from flask_login import login_required, current_user, logout_user -from server.models import Scores, Users, Sessions -from server.config import GAME_VERSION, MAX_TOP_SCORES +from .models import Scores, Users, Sessions +from .config import GAME_VERSION, MAX_TOP_SCORES blueprint = Blueprint("views", __name__)