From c299450d1c5c4e80e66ff560d97d39ee85944da0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Gdula?= Date: Tue, 13 Jun 2023 17:51:57 +0300 Subject: [PATCH] Add email column --- TFR/server/auth.py | 13 ++++++++----- TFR/server/config.py | 1 + TFR/server/models.py | 19 +++++++++++++++++++ TFR/server/templates/auth.html | 7 ++++++- docker-compose.yml | 4 ++-- 5 files changed, 36 insertions(+), 8 deletions(-) diff --git a/TFR/server/auth.py b/TFR/server/auth.py index f28d6a8..15350c1 100644 --- a/TFR/server/auth.py +++ b/TFR/server/auth.py @@ -7,7 +7,7 @@ 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 +from server.config import USER_REGEX, USER_EMAIL_REGEX blueprint = Blueprint("auth", __name__) @@ -22,16 +22,18 @@ def auth(): def register(): # Get the form data username = request.form["username"].strip() + email = request.form["email"].strip() password = request.form["password"].strip() - username_regex = re.compile(USER_REGEX) + 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 empty or invalid! Must be alphanumeric, and can contain ._-" - ) + 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: @@ -48,6 +50,7 @@ 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 95b118a..5d109ae 100644 --- a/TFR/server/config.py +++ b/TFR/server/config.py @@ -7,6 +7,7 @@ GAME_DIFFICULTIES = [0, 1, 2, 3, 4] USER_MAX_TOKENS = 3 USER_REGEX = r"\b[A-Za-z0-9._-]+\b" +USER_EMAIL_REGEX = r"[^@]+@[^@]+\.[^@]+" MAX_TOP_SCORES = 15 MAX_SEARCH_RESULTS = 5 diff --git a/TFR/server/models.py b/TFR/server/models.py index ea6d0d6..edddd0b 100644 --- a/TFR/server/models.py +++ b/TFR/server/models.py @@ -56,6 +56,23 @@ class Sessions(db.Model): ) +class Reset(db.Model): + """ + Password reset table + """ + + id = db.Column(db.Integer, primary_key=True) + + user_id = db.Column(db.Integer, db.ForeignKey("users.id", use_alter=True)) + reset_key = db.Column(db.String, nullable=False, unique=True) + + created_at = db.Column( + db.DateTime, + nullable=False, + server_default=db.func.now(), + ) + + class Users(db.Model, UserMixin): """ User table @@ -65,7 +82,9 @@ 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) password = db.Column(db.String, nullable=False) + joined_at = db.Column( db.DateTime, nullable=False, diff --git a/TFR/server/templates/auth.html b/TFR/server/templates/auth.html index 305ee79..df8b4e5 100644 --- a/TFR/server/templates/auth.html +++ b/TFR/server/templates/auth.html @@ -20,13 +20,18 @@

Register

-

Don't have an account? Register here!

+

Don't have an account?

+ + + + + diff --git a/docker-compose.yml b/docker-compose.yml index 3a6a0b9..e2e9a06 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -21,8 +21,8 @@ services: db: image: postgres:alpine restart: unless-stopped - ports: - - "5432:5432" +# ports: +# - "5432:5432" volumes: - ./Postgres/data:/var/lib/postgresql/data environment: