mirror of
https://github.com/fastapi-admin/fastapi-admin.git
synced 2026-03-13 10:32:25 +08:00
Make get_current_admin error to 401 and add 401 error page.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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}
|
||||
)
|
||||
|
||||
27
fastapi_admin/templates/errors/401.html
Normal file
27
fastapi_admin/templates/errors/401.html
Normal 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 %}
|
||||
Reference in New Issue
Block a user