feat(marketplace): add marketplace listings API and catalog items CRUD
This commit is contained in:
@@ -53,3 +53,33 @@ async def get_current_user(
|
||||
|
||||
|
||||
CurrentUser = Annotated[User, Depends(get_current_user)]
|
||||
|
||||
oauth2_scheme_optional = OAuth2PasswordBearer(tokenUrl="/api/auth/login", auto_error=False)
|
||||
|
||||
|
||||
async def get_optional_user(
|
||||
token: Annotated[str | None, Depends(oauth2_scheme_optional)],
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
) -> User | None:
|
||||
"""Resolve the JWT bearer token to a User if present and valid, otherwise None."""
|
||||
if not token:
|
||||
return None
|
||||
try:
|
||||
payload = jwt.decode(
|
||||
token, settings.public_pem, algorithms=[settings.ALGORITHM]
|
||||
)
|
||||
user_id: str | None = payload.get("sub")
|
||||
if user_id is None:
|
||||
return None
|
||||
user_id_int = int(user_id)
|
||||
except (JWTError, ValueError, TypeError):
|
||||
return None
|
||||
|
||||
result = await db.execute(select(User).where(User.id == user_id_int))
|
||||
user = result.scalar_one_or_none()
|
||||
if user is None or not user.is_active:
|
||||
return None
|
||||
return user
|
||||
|
||||
|
||||
OptionalUser = Annotated[User | None, Depends(get_optional_user)]
|
||||
|
||||
Reference in New Issue
Block a user