Improve super admin login ntfy alert with IP, user-agent, and full title

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-23 08:40:56 -07:00
parent 9deee8d42e
commit 5e79a66f2d

View File

@@ -1,5 +1,5 @@
import logging
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi import APIRouter, Depends, HTTPException, Request, status
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, delete
@@ -16,16 +16,18 @@ settings = get_settings()
@router.post("/login")
async def admin_login(body: dict):
async def admin_login(body: dict, request: Request):
username = body.get("username", "")
password = body.get("password", "")
if username != settings.admin_username or password != settings.admin_password:
logger.warning("Failed super-admin login attempt for username=%s", username)
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid admin credentials")
token = create_admin_token({"sub": "admin"})
ip = request.headers.get("X-Forwarded-For", request.client.host if request.client else "unknown").split(",")[0].strip()
ua = request.headers.get("User-Agent", "unknown")
await notify(
title="Super Admin Login",
message=f"Admin logged in as: {username}",
title="Homeschool Dashboard Super Admin Login",
message=f"User: {username}\nIP: {ip}\nUA: {ua}",
priority="high",
tags=["key"],
)