Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__,
Expand All @@ -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')

Expand Down
25 changes: 20 additions & 5 deletions backend/routes/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,48 @@
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

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',
Expand Down
2 changes: 2 additions & 0 deletions run.py
Original file line number Diff line number Diff line change
@@ -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)