34 lines
No EOL
1.1 KiB
Python
34 lines
No EOL
1.1 KiB
Python
from flask import Blueprint, request, jsonify
|
|
from models.user import User, db
|
|
|
|
auth_bp = Blueprint('auth', __name__)
|
|
|
|
@auth_bp.route('/signup', methods=['POST'])
|
|
def signup():
|
|
data = request.json
|
|
username = data.get('username')
|
|
password = data.get('password')
|
|
|
|
if not username or not password:
|
|
return jsonify({"error": "Username and password are required"}), 400
|
|
|
|
if User.query.filter_by(username=username).first():
|
|
return jsonify({"error": "Username already exists"}), 400
|
|
|
|
User.create_user(username, password)
|
|
return jsonify({"message": "User registered successfully"}), 201
|
|
|
|
@auth_bp.route('/login', methods=['POST'])
|
|
def login():
|
|
data = request.json
|
|
username = data.get('username')
|
|
password = data.get('password')
|
|
|
|
if not username or not password:
|
|
return jsonify({"error": "Username and password are required"}), 400
|
|
|
|
user = User.verify_user(username, password)
|
|
if not user:
|
|
return jsonify({"error": "Invalid username or password"}), 401
|
|
|
|
return jsonify({"message": f"Welcome, {username}!"}), 200 |