Add admin index redirect for /admin/ route
This commit is contained in:
11
app/admin.py
11
app/admin.py
@@ -86,6 +86,7 @@ class EnvCredentialProvider(Provider):
|
|||||||
await super().register(app)
|
await super().register(app)
|
||||||
# Keep explicit assignment for compatibility across fastapi-admin versions.
|
# Keep explicit assignment for compatibility across fastapi-admin versions.
|
||||||
app.login_provider = self
|
app.login_provider = self
|
||||||
|
app.get("/")(self.index_view)
|
||||||
app.get(self.login_path)(self.login_view)
|
app.get(self.login_path)(self.login_view)
|
||||||
app.post(self.login_path)(self.login)
|
app.post(self.login_path)(self.login)
|
||||||
app.get(self.logout_path)(self.logout)
|
app.get(self.logout_path)(self.logout)
|
||||||
@@ -136,6 +137,16 @@ class EnvCredentialProvider(Provider):
|
|||||||
continue
|
continue
|
||||||
return f"{admin_path}{getattr(request.app.login_provider, 'login_path', '/login')}"
|
return f"{admin_path}{getattr(request.app.login_provider, 'login_path', '/login')}"
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _login_url(request: Request) -> str:
|
||||||
|
return request.app.admin_path.rstrip("/") + "/login"
|
||||||
|
|
||||||
|
async def index_view(self, request: Request):
|
||||||
|
# fastapi-admin has no default "/" page in this setup.
|
||||||
|
if getattr(request.state, "admin", None):
|
||||||
|
return RedirectResponse(url=self._admin_home(request), status_code=HTTP_303_SEE_OTHER)
|
||||||
|
return RedirectResponse(url=self._login_url(request), status_code=HTTP_303_SEE_OTHER)
|
||||||
|
|
||||||
async def login_view(self, request: Request):
|
async def login_view(self, request: Request):
|
||||||
return self._template_response(
|
return self._template_response(
|
||||||
request=request,
|
request=request,
|
||||||
|
|||||||
Reference in New Issue
Block a user