CoolFace
Apppublic

umarani18/url-malicious-api

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
auth.py85 linesDownload Raw Back to root
1from flask import Blueprint, request, jsonify2from extensions import db, jwt3from models import User4from flask_jwt_extended import create_access_token, jwt_required, get_jwt_identity5import re6 7auth_bp = Blueprint('auth', __name__)8 9# Basic email validation10def is_valid_email(email):11    return re.match(r"[^@]+@[^@]+\.[^@]+", email)12 13@auth_bp.route('/register', methods=['POST'])14def register():15    data = request.get_json()16    17    # Required fields18    full_name = data.get('full_name')19    email = data.get('email')20    password = data.get('password')21    confirm_password = data.get('confirm_password')22 23    # Basic validations24    if not all([full_name, email, password, confirm_password]):25        return jsonify({"error": "All fields are required (full_name, email, password, confirm_password)"}), 40026 27    if not is_valid_email(email):28        return jsonify({"error": "Invalid email format"}), 40029 30    if password != confirm_password:31        return jsonify({"error": "Passwords do not match"}), 40032 33    if len(password) < 6:34        return jsonify({"error": "Password must be at least 6 characters long"}), 40035 36    # Check if user already exists37    if User.query.filter_by(email=email).first():38        return jsonify({"error": "Email already registered"}), 40039 40    try:41        new_user = User(full_name=full_name, email=email, password=password)42        db.session.add(new_user)43        db.session.commit()44        45        return jsonify({46            "success": True, 47            "message": "User registered successfully",48            "user": new_user.to_dict()49        }), 20150    except Exception as e:51        db.session.rollback()52        return jsonify({"error": str(e)}), 50053 54@auth_bp.route('/login', methods=['POST'])55def login():56    data = request.get_json()57    email = data.get('email')58    password = data.get('password')59 60    if not email or not password:61        return jsonify({"error": "Email and password are required"}), 40062 63    user = User.query.filter_by(email=email).first()64 65    if user and user.check_password(password):66        # Create token67        access_token = create_access_token(identity=str(user.id))68        return jsonify({69            "success": True,70            "message": "Login successful",71            "access_token": access_token,72            "user": user.to_dict()73        }), 20074    else:75        return jsonify({"error": "Invalid email or password"}), 40176 77@auth_bp.route('/profile', methods=['GET'])78@jwt_required()79def profile():80    user_id = get_jwt_identity()81    user = User.query.get(user_id)82    if user:83        return jsonify({"success": True, "user": user.to_dict()}), 20084    return jsonify({"error": "User not found"}), 40485