Created
February 27, 2025 21:39
-
-
Save mypy-play/90eb6769565457c2934d7719e53ccb0b to your computer and use it in GitHub Desktop.
Shared via mypy Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections.abc import Callable | |
from functools import wraps | |
from typing import ParamSpec, TypeAlias | |
from flask import Response | |
from flask_login import current_user | |
P = ParamSpec("P") | |
FlaskRestfulHTTPMethodResponse: TypeAlias = tuple[Response | dict, int] | |
FlaskRestfulHTTPMethod: TypeAlias = Callable[P, FlaskRestfulHTTPMethodResponse] | |
def check_auth(f: FlaskRestfulHTTPMethod) -> FlaskRestfulHTTPMethod: | |
@wraps(f) | |
def wrapper(*args: P.args, **kwargs: P.kwargs) -> FlaskRestfulHTTPMethodResponse: | |
if not current_user.is_authenticated: | |
return {"message": "Unauthorized"}, 401 | |
return f(*args, **kwargs) | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment