Skip to content

Commit

Permalink
feat: complete RegisterForm validation and add validation for LoginForm
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaVls committed Sep 28, 2021
1 parent c4c3494 commit 356eadd
Showing 1 changed file with 71 additions and 7 deletions.
78 changes: 71 additions & 7 deletions src/typefight/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
Form, StringField, PasswordField, SelectField, validators, ValidationError
)

from werkzeug.security import check_password_hash
from psycopg2.extras import RealDictCursor

from typefight.utils import get_countries_list, get_countries_path
from typefight.db import get_db

import contextlib

# ==========[ REGISTRATION FORM ]========== #
countries = get_countries_list(get_countries_path())
for i in range(0, len(countries)):
countries[i] = (countries[i], countries[i])
countries.insert(0, ("", "Select a country..."))

def existing_player_check(form, field):
def existing_player_check(_, field):
db = get_db()

with contextlib.closing(db.cursor()) as cur:
Expand All @@ -24,18 +29,77 @@ def existing_player_check(form, field):
)

if cur.fetchone() is not None:
raise ValidationError(f"Player name {field.data} already exists")
raise ValidationError(f'Player name "{field.data}" already exists')

class RegistrationForm(Form):
username = StringField("Username", [
username = StringField(
"Username",
[
validators.DataRequired(message="A username is required"),
validators.Length(max=15, message="Keep the username below 15 characters"),
validators.Regexp(r"^\w+$", message='Only letters, numbers, and underscores'),
existing_player_check
])
password = PasswordField("Password", [
],
render_kw={"placeholder": "el_Camino23"}
)

password = PasswordField(
"Password",
[
validators.DataRequired(message="A password is required"),
validators.Length(min=6, message="Password must be at least 6 characters long"),
validators.EqualTo("confirm", message="Passwords don't match")
])
confirm = PasswordField("Confirm Password")
],
render_kw={"placeholder": "safePassword123"}
)

confirm = PasswordField(
"Confirm Password",
render_kw={"placeholder": "safePassword123"}
)

country = SelectField("Country", choices=countries)


# ==========[ LOGIN FORM ]========== #
def database_check(form, field):
db = get_db()

with contextlib.closing(db.cursor(cursor_factory=RealDictCursor)) as cur:
cur.execute(
"""
SELECT *
FROM players
WHERE player_name = %s
""", (form.username.data, )
)
player = cur.fetchone()

if player is None:
if field.id == "username":
# this is to show the error message under the correct input
raise ValidationError(f'Username "{field.data}" not found')
else:
if field.id == "password":
if not check_password_hash(player["pass_hash"], field.data + player["salt"]):
raise ValidationError("Incorrect password")

class LoginForm(Form):
username = StringField(
"Username",
[
validators.DataRequired(message="A username is required"),
validators.Length(max=15, message="Usernames are below 15 characters"),
database_check
],
render_kw={"placeholder": "registered_Player23"}
)

password = PasswordField(
"Password",
[
validators.DataRequired(message="A password is required"),
database_check
],
render_kw={"placeholder": "safePassword123"}
)

0 comments on commit 356eadd

Please sign in to comment.