|
8 | 8 | from fastapi.exceptions import RequestValidationError, HTTPException, StarletteHTTPException
|
9 | 9 | from sqlmodel import Session
|
10 | 10 | from routers import authentication, organization, role, user
|
11 |
| -from utils.auth import get_authenticated_user, get_optional_user, NeedsNewTokens, get_user_from_reset_token, PasswordValidationError |
| 11 | +from utils.auth import get_authenticated_user, get_optional_user, NeedsNewTokens, get_user_from_reset_token, PasswordValidationError, AuthenticationError |
12 | 12 | from utils.models import User
|
13 | 13 | from utils.db import get_session, set_up_db
|
14 | 14 |
|
@@ -37,6 +37,15 @@ async def lifespan(app: FastAPI):
|
37 | 37 | # -- Exception Handling Middlewares --
|
38 | 38 |
|
39 | 39 |
|
| 40 | +# Handle AuthenticationError by redirecting to login page |
| 41 | +@app.exception_handler(AuthenticationError) |
| 42 | +async def authentication_error_handler(request: Request, exc: AuthenticationError): |
| 43 | + return RedirectResponse( |
| 44 | + url="/login", |
| 45 | + status_code=status.HTTP_303_SEE_OTHER |
| 46 | + ) |
| 47 | + |
| 48 | + |
40 | 49 | # Handle NeedsNewTokens by setting new tokens and redirecting to same page
|
41 | 50 | @app.exception_handler(NeedsNewTokens)
|
42 | 51 | async def needs_new_tokens_handler(request: Request, exc: NeedsNewTokens):
|
@@ -104,10 +113,6 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE
|
104 | 113 | # Handle StarletteHTTPException (including 404, 405, etc.) by rendering the error page
|
105 | 114 | @app.exception_handler(StarletteHTTPException)
|
106 | 115 | async def http_exception_handler(request: Request, exc: StarletteHTTPException):
|
107 |
| - # Don't handle redirects |
108 |
| - if exc.status_code in [301, 302, 303, 307, 308]: |
109 |
| - raise exc |
110 |
| - |
111 | 116 | return templates.TemplateResponse(
|
112 | 117 | request,
|
113 | 118 | "errors/error.html",
|
|
0 commit comments