"""Whoami endpoint: lets the single-page app detect the login state.""" from __future__ import annotations from kaya.core import HttpContext from kaya.openapi import operation from ..app import app, oidc_mixin from ..auth import display_name, require_auth from ..http import send_json @app.GET("/api/me") @operation(summary="Current user", description="Returns the authenticated user's identity from the " "session; 401 when not logged in.", tags=["auth"], responses={ 200: {"description": "The current user"}, 401: {"description": "Not logged in"}, }) @require_auth async def me(ctx: HttpContext) -> None: user = oidc_mixin.get_user(ctx) assert user is not None # enforced by @require_auth await send_json(ctx, 200, {"sub": user.sub, "name": display_name(user)})