(feat): Basic user auth
This commit is contained in:
parent
e5976d3c8d
commit
de460fbcbc
6 changed files with 90 additions and 9 deletions
34
routes/auth.py
Normal file
34
routes/auth.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue