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

24 lines
577 B
Python

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