added gravatar to user and added /api/whoami call

This commit is contained in:
Christian Krakau-Louis
2025-03-25 21:31:32 +01:00
parent 657861db26
commit e4101f690b
3 changed files with 56 additions and 3 deletions
+32
View File
@@ -0,0 +1,32 @@
from fastapi import APIRouter, Request, HTTPException, status
from hashlib import md5
router = APIRouter()
@router.get("/whoami")
async def whoami(request: Request):
"""
Returns user info if logged in, else 401.
Example response:
{
"email": "someone@example.com",
"picture": "https://www.gravatar.com/avatar/..."
}
"""
user = request.session.get("user")
if not user:
raise HTTPException(status_code=401, detail="Not logged in")
email = user.get("email")
if not email:
raise HTTPException(status_code=400, detail="User has no email in session")
# Generate Gravatar URL from email
# For more options, see: https://en.gravatar.com/site/implement/images/
email_hash = md5(email.strip().lower().encode()).hexdigest()
gravatar_url = f"https://www.gravatar.com/avatar/{email_hash}?d=identicon"
return {
"email": email,
"picture": gravatar_url
}