From fb1761120725ac497c3945d1d2b2020faa375eae Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Thu, 4 Jul 2024 17:24:20 +0300 Subject: [PATCH] [feat] add `authenticated_user_id` decorator (#2566) * feat: add `optional_user_id` decorator --- lnbits/decorators.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lnbits/decorators.py b/lnbits/decorators.py index f6d04b1b7..f61c6c028 100644 --- a/lnbits/decorators.py +++ b/lnbits/decorators.py @@ -158,6 +158,19 @@ async def check_user_exists( return user +async def optional_user_id( + access_token: Annotated[Optional[str], Depends(check_access_token)], + usr: Optional[UUID4] = None, +) -> Optional[str]: + if usr and settings.is_auth_method_allowed(AuthMethods.user_id_only): + return usr.hex + if access_token: + account = await _get_account_from_token(access_token) + return account.id if account else None + + return None + + async def check_admin(user: Annotated[User, Depends(check_user_exists)]) -> User: if user.id != settings.super_user and user.id not in settings.lnbits_admin_users: raise HTTPException(