init: TEALab 2025 实验室网站项目
从百度网盘 /apps/bypy/dofile/ 下载的完整项目结构,包含: - dofile/: Flask 后端(端口 5000)+ 静态前端(端口 6002) - officefile/: 项目相关文档 - .gitignore: Python venv / 日志 / 缓存 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
from flask_login import login_user, logout_user, login_required, current_user
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 添加父目录到路径
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
from models import db, User
|
||||
from werkzeug.security import check_password_hash
|
||||
|
||||
bp = Blueprint('auth', __name__)
|
||||
|
||||
@bp.route('/register', methods=['POST'])
|
||||
def register():
|
||||
"""用户注册"""
|
||||
data = request.get_json()
|
||||
|
||||
if not data or not data.get('username') or not data.get('email') or not data.get('password'):
|
||||
return jsonify({'error': 'Missing required fields'}), 400
|
||||
|
||||
# 检查用户名是否已存在
|
||||
if User.query.filter_by(username=data['username']).first():
|
||||
return jsonify({'error': 'Username already exists'}), 400
|
||||
|
||||
# 检查邮箱是否已存在
|
||||
if User.query.filter_by(email=data['email']).first():
|
||||
return jsonify({'error': 'Email already exists'}), 400
|
||||
|
||||
# 创建新用户
|
||||
user = User(
|
||||
username=data['username'],
|
||||
email=data['email'],
|
||||
is_admin=data.get('is_admin', False)
|
||||
)
|
||||
user.set_password(data['password'])
|
||||
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({
|
||||
'message': 'User registered successfully',
|
||||
'user': user.to_dict()
|
||||
}), 201
|
||||
|
||||
@bp.route('/login', methods=['POST'])
|
||||
def login():
|
||||
"""用户登录"""
|
||||
data = request.get_json()
|
||||
|
||||
if not data or not data.get('username') or not data.get('password'):
|
||||
return jsonify({'error': 'Username and password required'}), 400
|
||||
|
||||
user = User.query.filter_by(username=data['username']).first()
|
||||
|
||||
if user and user.check_password(data['password']):
|
||||
login_user(user, remember=data.get('remember', False))
|
||||
return jsonify({
|
||||
'message': 'Login successful',
|
||||
'user': user.to_dict()
|
||||
}), 200
|
||||
else:
|
||||
return jsonify({'error': 'Invalid username or password'}), 401
|
||||
|
||||
@bp.route('/logout', methods=['POST'])
|
||||
@login_required
|
||||
def logout():
|
||||
"""用户登出"""
|
||||
logout_user()
|
||||
return jsonify({'message': 'Logout successful'}), 200
|
||||
|
||||
@bp.route('/me', methods=['GET'])
|
||||
@login_required
|
||||
def get_current_user():
|
||||
"""获取当前登录用户信息"""
|
||||
return jsonify({'user': current_user.to_dict()}), 200
|
||||
|
||||
@bp.route('/check', methods=['GET'])
|
||||
def check_auth():
|
||||
"""检查认证状态"""
|
||||
if current_user.is_authenticated:
|
||||
return jsonify({
|
||||
'authenticated': True,
|
||||
'user': current_user.to_dict()
|
||||
}), 200
|
||||
else:
|
||||
return jsonify({'authenticated': False}), 200
|
||||
|
||||
Reference in New Issue
Block a user