8000 Shared via mypy Playground · GitHub
[go: up one dir, main page]

Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created February 27, 2025 21:39
Show Gist options
  • Save mypy-play/90eb6769565457c2934d7719e53ccb0b to your computer and use it in GitHub Desktop.
8000
Save mypy-play/90eb6769565457c2934d7719e53ccb0b to your computer and use it in GitHub Desktop.
Shared via mypy Playground
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
0