fix:user reg email and username

manzilcheck
Kushal Dotel 6 months ago
parent fd2fe03561
commit 47fc49ff9f
  1. 4
      backend/app.py
  2. 18
      backend/blueprints/profile/__init__.py
  3. 2
      backend/config.py
  4. 2
      backend/db/model.py
  5. 2
      backend/utils/utils.py

@ -34,8 +34,8 @@ def seed_data():
try: try:
# Drop and recreate schema (only for testing, not recommended in production) # Drop and recreate schema (only for testing, not recommended in production)
sql = text('DROP SCHEMA public CASCADE; CREATE SCHEMA public;') sql = text('DROP SCHEMA public CASCADE; CREATE SCHEMA public;')
with db.engine.connect() as connection: db.session.execute(sql)
connection.execute(sql) db.session.commit()
db.create_all() # Recreate tables db.create_all() # Recreate tables
# Define roles and constants (ensure UserRole.USER is an integer or map it) # Define roles and constants (ensure UserRole.USER is an integer or map it)

@ -8,6 +8,8 @@ from werkzeug.security import generate_password_hash
import uuid import uuid
import os import os
from config import * from config import *
from utils.utils import password_check_sanity,is_valid_email,InsecurePasswordException
from sqlalchemy.exc import IntegrityError
profile = Blueprint('profile', __name__) profile = Blueprint('profile', __name__)
@ -33,6 +35,16 @@ def register():
is_activated = True # New user will be activated initially is_activated = True # New user will be activated initially
profile_picture = request.files.get('profile_picture') profile_picture = request.files.get('profile_picture')
# Validate email
if not is_valid_email(email):
return jsonify({"error": "Invalid email address"}), 400
# Validate password
try:
password_check_sanity(password)
except InsecurePasswordException as e:
return jsonify({"error": str(e)}), 400
# Validate required fields # Validate required fields
if not all([email, first_name, last_name, username, password]): if not all([email, first_name, last_name, username, password]):
return jsonify({"error": "Missing required fields"}), 400 return jsonify({"error": "Missing required fields"}), 400
@ -78,7 +90,11 @@ def register():
try: try:
db.session.add(new_user) db.session.add(new_user)
db.session.commit() db.session.commit()
return jsonify({"message": "User registered successfully. Please check your email to activate your account."}), 201
return jsonify({"message": "User registered successfully."}), 201
except IntegrityError as e:
db.session.rollback()
return jsonify({"error": "User with this email or username already exists."}), 400
except Exception as e: except Exception as e:
db.session.rollback() db.session.rollback()
return jsonify({"error": "Registration failed, please try again later."}), 500 return jsonify({"error": "Registration failed, please try again later."}), 500

@ -15,7 +15,7 @@ DB_NAME: str = "educonnect"
DEFAULT_PROFILE_FILE: str = "defaultUserBanner.png" DEFAULT_PROFILE_FILE: str = "defaultUserBanner.png"
DEFAULT_COURSE_COVER: str = "defaultCourseCover.png" DEFAULT_COURSE_COVER: str = "defaultCourseCover.png"
DEFAULT_BADGE_ICON: str = "defaultBadgeIcon.png" DEFAULT_BADGE_ICON: str = "defaultBadgeIcon.png"
DISABLE_PASSWORD_SANITY_CHECKS: bool = True DISABLE_PASSWORD_SANITY_CHECKS: bool = False
PROJECT_ROOT: os.path = os.path.dirname(os.path.abspath(__file__)) PROJECT_ROOT: os.path = os.path.dirname(os.path.abspath(__file__))
USER_UPLOADS_DIR: str = os.path.join(PROJECT_ROOT, "uploads") USER_UPLOADS_DIR: str = os.path.join(PROJECT_ROOT, "uploads")

@ -19,7 +19,7 @@ class User(db.Model):
email: Mapped[str] = mapped_column(String(64), nullable=False, unique=True) email: Mapped[str] = mapped_column(String(64), nullable=False, unique=True)
firstName: Mapped[str] = mapped_column(String(32), nullable=False) firstName: Mapped[str] = mapped_column(String(32), nullable=False)
lastName: Mapped[str] = mapped_column(String(32), nullable=False) lastName: Mapped[str] = mapped_column(String(32), nullable=False)
username: Mapped[str] = mapped_column(String(32), nullable=False) username: Mapped[str] = mapped_column(String(32), nullable=False, unique=True)
hash_password: Mapped[str] = mapped_column(String(256), nullable=False) hash_password: Mapped[str] = mapped_column(String(256), nullable=False)
activationKey: Mapped[str] = mapped_column(String(128), nullable=False) activationKey: Mapped[str] = mapped_column(String(128), nullable=False)
sessions: Mapped[List["Session"]] = relationship(back_populates="user", cascade="all, delete-orphan") sessions: Mapped[List["Session"]] = relationship(back_populates="user", cascade="all, delete-orphan")

@ -33,7 +33,7 @@ def is_valid_email(email):
else: else:
return False return False
def password_check(passwd: str) -> bool: def password_check_sanity(passwd: str) -> bool:
if DISABLE_PASSWORD_SANITY_CHECKS: if DISABLE_PASSWORD_SANITY_CHECKS:
return True return True
special_symbol_pattern = r'[$@#%]' special_symbol_pattern = r'[$@#%]'

Loading…
Cancel
Save