Tweak admin login logic

main
Thomas Sileo 2022-09-04 09:24:58 +02:00
parent f7671f0585
commit e454e8fe84
1 changed files with 8 additions and 12 deletions

View File

@ -34,6 +34,7 @@ from app.config import verify_password
from app.database import AsyncSession from app.database import AsyncSession
from app.database import get_db_session from app.database import get_db_session
from app.lookup import lookup from app.lookup import lookup
from app.templates import is_current_user_admin
from app.uploads import save_upload from app.uploads import save_upload
from app.utils import pagination from app.utils import pagination
from app.utils.emoji import EMOJIS_BY_NAME from app.utils.emoji import EMOJIS_BY_NAME
@ -68,16 +69,6 @@ router = APIRouter(
unauthenticated_router = APIRouter() unauthenticated_router = APIRouter()
@router.get("/")
async def admin_index(
request: Request,
db_session: AsyncSession = Depends(get_db_session),
) -> templates.TemplateResponse:
return await templates.render_template(
db_session, request, "index.html", {"request": request}
)
@router.get("/lookup") @router.get("/lookup")
async def get_lookup( async def get_lookup(
request: Request, request: Request,
@ -1108,7 +1099,10 @@ async def admin_actions_vote(
async def login( async def login(
request: Request, request: Request,
db_session: AsyncSession = Depends(get_db_session), db_session: AsyncSession = Depends(get_db_session),
) -> templates.TemplateResponse: ) -> templates.TemplateResponse | RedirectResponse:
if is_current_user_admin(request):
return RedirectResponse(request.url_for("admin_stream"), status_code=302)
return await templates.render_template( return await templates.render_template(
db_session, db_session,
request, request,
@ -1142,7 +1136,9 @@ async def login_validation(
status_code=403, status_code=403,
) )
resp = RedirectResponse(redirect or "/admin/stream", status_code=302) resp = RedirectResponse(
redirect or request.url_for("admin_stream"), status_code=302
)
resp.set_cookie("session", session_serializer.dumps({"is_logged_in": True})) # type: ignore # noqa: E501 resp.set_cookie("session", session_serializer.dumps({"is_logged_in": True})) # type: ignore # noqa: E501
return resp return resp