|
3 | 3 | from typing import Optional
|
4 | 4 | from urllib.parse import urlparse
|
5 | 5 | from datetime import datetime
|
6 |
| -from fastapi import APIRouter, Depends, HTTPException, BackgroundTasks, Form, Request |
| 6 | +from fastapi import APIRouter, Depends, BackgroundTasks, Form, Request |
7 | 7 | from fastapi.responses import RedirectResponse
|
8 | 8 | from fastapi.templating import Jinja2Templates
|
9 | 9 | from pydantic import BaseModel, EmailStr, ConfigDict
|
|
28 | 28 | PasswordValidationError,
|
29 | 29 | get_optional_user
|
30 | 30 | )
|
| 31 | +from exceptions.http_exceptions import EmailAlreadyRegisteredError, CredentialsError |
31 | 32 |
|
32 | 33 | logger = getLogger("uvicorn.error")
|
33 | 34 |
|
|
37 | 38 | # --- Custom Exceptions ---
|
38 | 39 |
|
39 | 40 |
|
40 |
| -class EmailAlreadyRegisteredError(HTTPException): |
41 |
| - def __init__(self): |
42 |
| - super().__init__( |
43 |
| - status_code=409, |
44 |
| - detail="This email is already registered" |
45 |
| - ) |
46 |
| - |
47 | 41 |
|
48 |
| -class AuthenticationError(HTTPException): |
49 |
| - def __init__(self, message: str = "Invalid credentials"): |
50 |
| - super().__init__( |
51 |
| - status_code=401, |
52 |
| - detail=message |
53 |
| - ) |
54 | 42 |
|
55 | 43 |
|
56 | 44 | # --- Server Request and Response Models ---
|
@@ -246,7 +234,7 @@ async def read_reset_password(
|
246 | 234 |
|
247 | 235 | # Raise informative error to let user know the token is invalid and may have expired
|
248 | 236 | if not authorized_user:
|
249 |
| - raise HTTPException(status_code=400, detail="Invalid or expired token") |
| 237 | + raise CredentialsError(message="Invalid or expired token") |
250 | 238 |
|
251 | 239 | return templates.TemplateResponse(
|
252 | 240 | "authentication/reset_password.html",
|
@@ -310,7 +298,7 @@ async def login(
|
310 | 298 | User.email == user.email)).first()
|
311 | 299 |
|
312 | 300 | if not db_user or not db_user.password or not verify_password(user.password, db_user.password.hashed_password):
|
313 |
| - raise AuthenticationError() |
| 301 | + raise CredentialsError() |
314 | 302 |
|
315 | 303 | # Create access token
|
316 | 304 | access_token = create_access_token(
|
@@ -416,7 +404,7 @@ async def reset_password(
|
416 | 404 | user.email, user.token, session)
|
417 | 405 |
|
418 | 406 | if not authorized_user or not reset_token:
|
419 |
| - raise AuthenticationError("Invalid or expired password reset token; please request a new one") |
| 407 | + raise CredentialsError("Invalid or expired password reset token; please request a new one") |
420 | 408 |
|
421 | 409 | # Update password and mark token as used
|
422 | 410 | if authorized_user.password:
|
@@ -488,7 +476,7 @@ async def confirm_email_update(
|
488 | 476 | )
|
489 | 477 |
|
490 | 478 | if not user or not update_token:
|
491 |
| - raise AuthenticationError("Invalid or expired email update token; please request a new one") |
| 479 | + raise CredentialsError("Invalid or expired email update token; please request a new one") |
492 | 480 |
|
493 | 481 | # Update email and mark token as used
|
494 | 482 | user.email = new_email
|
|
0 commit comments