Make get_current_admin error to 401 and add 401 error page.

This commit is contained in:
long2ice
2021-08-31 11:27:23 +08:00
parent 2fcbc814de
commit e99d673191
5 changed files with 44 additions and 3 deletions

View File

@@ -3,7 +3,7 @@ from typing import List, Optional, Type
from fastapi import Depends, HTTPException
from fastapi.params import Path
from starlette.requests import Request
from starlette.status import HTTP_404_NOT_FOUND
from starlette.status import HTTP_401_UNAUTHORIZED, HTTP_404_NOT_FOUND
from tortoise import Tortoise
from fastapi_admin.exceptions import InvalidResource
@@ -68,5 +68,5 @@ def get_redis(request: Request):
def get_current_admin(request: Request):
admin = request.state.admin
if not admin:
raise HTTPException(status_code=HTTP_404_NOT_FOUND)
raise HTTPException(status_code=HTTP_401_UNAUTHORIZED)
return admin

View File

@@ -54,3 +54,9 @@ async def forbidden_error_exception(request: Request, exc: HTTPException):
return templates.TemplateResponse(
"errors/403.html", status_code=exc.status_code, context={"request": request}
)
async def unauthorized_error_exception(request: Request, exc: HTTPException):
return templates.TemplateResponse(
"errors/401.html", status_code=exc.status_code, context={"request": request}
)

View File

@@ -0,0 +1,27 @@
{% extends 'base.html' %}
{% block body %}
<div class="page page-center">
<div class="container-tight py-4">
<div class="empty">
<div class="empty-header">401</div>
<p class="empty-title">Oops… You are unauthorized</p>
<p class="empty-subtitle text-muted">
We are sorry but the page you are looking for need authorization.
</p>
<div class="empty-action">
<a href="{{ request.app.admin_path }}" class="btn btn-primary">
<svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24"
stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<line x1="5" y1="12" x2="19" y2="12"/>
<line x1="5" y1="12" x2="11" y2="18"/>
<line x1="5" y1="12" x2="11" y2="6"/>
</svg>
{{ _('return_home') }}
</a>
</div>
</div>
</div>
</div>
{% endblock %}