pzx-web-api/app/util/auth.py

24 lines
577 B
Python
Raw Normal View History

2023-08-12 20:15:48 +08:00
from functools import wraps
from flask import request, session
from app.api.v1.exception.auth import AuthError
def login_required(f):
"""
登陆保护验证用户是否登陆
"""
from app import rpc
@wraps(f)
def wrapper(*args, **kwargs):
token = request.headers.get("Authorization", default=None)
if not token:
raise AuthError()
user_id = rpc.admin.identify(token)
if not user_id:
raise AuthError()
session['user_id'] = user_id
return f(*args, **kwargs)
return wrapper