diff --git a/backend/app.py b/backend/app.py index 892d585..83c00db 100644 --- a/backend/app.py +++ b/backend/app.py @@ -3,6 +3,7 @@ from flask_jwt_extended import JWTManager from flask_cors import CORS from backend.database import db, migrate +from backend.routes.auth import auth_bp def create_app(config_class): app = Flask(__name__, @@ -26,9 +27,8 @@ def index(): def api_status(): return jsonify({'message': 'API is working', 'status': 'success'}), 200 - # Register blueprints - from backend.routes.auth import auth_bp - + + app.register_blueprint(auth_bp, url_prefix='/api/auth') diff --git a/backend/routes/auth.py b/backend/routes/auth.py index 54bbfa1..016ad4d 100644 --- a/backend/routes/auth.py +++ b/backend/routes/auth.py @@ -2,17 +2,32 @@ from flask_jwt_extended import create_access_token, jwt_required, get_jwt_identity from backend.models.user import User from backend.database import db +import re auth_bp = Blueprint('auth', __name__) +def is_password_strong(password): + return len(password) >= 6 @auth_bp.route('/register', methods=['POST']) def register(): data = request.get_json() - + + username = data.get('username') + email = data.get('email') + password = data.get('password') # Validate input if not data.get('username') or not data.get('email') or not data.get('password'): return jsonify({'error': 'Missing required fields'}), 400 + # Check the format of email + if not re.match(r"[^@]+@[^@]+\.[^@]+", email): + return jsonify({'error': "Invalid Email format"}), 400 + + # check if password is strong or not + if not is_password_strong(password): + return jsonify({'error': 'Password too weak (min 6 char)'}), 400 + + # Check if user exists if User.query.filter_by(email=data['email']).first(): return jsonify({'error': 'Email already registered'}), 400 @@ -20,15 +35,15 @@ def register(): if User.query.filter_by(username=data['username']).first(): return jsonify({'error': 'Username already taken'}), 400 - # Create new user - user = User(username=data['username'], email=data['email']) - user.set_password(data['password']) + + user = User(username=username, email=email) + user.set_password(password) db.session.add(user) db.session.commit() # Create access token - access_token = create_access_token(identity=user.id) + access_token = create_access_token(identity= user.id) return jsonify({ 'message': 'User created successfully', diff --git a/run.py b/run.py index 9949b55..d4d7953 100644 --- a/run.py +++ b/run.py @@ -1,8 +1,10 @@ from backend.app import create_app from config import config +from backend.routes.auth import auth_bp import os if __name__ == '__main__': config_name = os.environ.get('FLASK_ENV') or 'default' app = create_app(config[config_name]) app.run(host='0.0.0.0', port=4000, debug=True) + app.register_blueprint(auth_bp) \ No newline at end of file