From ebed6c0cb14086d8cd6cc46a6a4fc3f121ae4cde Mon Sep 17 00:00:00 2001 From: Vineeth Voruganti <13438633+VVoruganti@users.noreply.github.com> Date: Fri, 25 Oct 2024 12:53:05 -0400 Subject: [PATCH] feat(api) added caching for token validation --- api/security.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/api/security.py b/api/security.py index eeab743..ab493ad 100644 --- a/api/security.py +++ b/api/security.py @@ -1,6 +1,7 @@ from os import getenv from typing import Optional +from functools import lru_cache from dotenv import load_dotenv from fastapi import Depends, HTTPException from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer @@ -24,6 +25,11 @@ class User(BaseModel): email: Optional[str] +@lru_cache(maxsize=50) +def validate_user(token: str): + return supabase.auth.get_user(token) + + async def get_current_user( credentials: HTTPAuthorizationCredentials = Depends(security), ) -> User: @@ -35,7 +41,7 @@ async def get_current_user( token = credentials.credentials # Verify with Supabase - user = supabase.auth.get_user(token) + user = validate_user(token) if not user: raise HTTPException(