Spaces:
Running
Running
Deploy backend from GitHub Actions
Browse files- .env.example +1 -0
- backend/api/v1/routers/auth.py +14 -6
- backend/core/config.py +1 -0
.env.example
CHANGED
|
@@ -9,6 +9,7 @@ ALLOWED_ORIGIN_REGEX=https://.*\.vercel\.app
|
|
| 9 |
TRUSTED_HOSTS=localhost,127.0.0.1,*.vercel.app,*.hf.space
|
| 10 |
FRONTEND_URL=http://localhost:3000
|
| 11 |
BACKEND_URL=http://localhost:8000
|
|
|
|
| 12 |
VERSION=1.0.0
|
| 13 |
|
| 14 |
# === DATABASE ===
|
|
|
|
| 9 |
TRUSTED_HOSTS=localhost,127.0.0.1,*.vercel.app,*.hf.space
|
| 10 |
FRONTEND_URL=http://localhost:3000
|
| 11 |
BACKEND_URL=http://localhost:8000
|
| 12 |
+
REQUIRE_EMAIL_VERIFICATION=false
|
| 13 |
VERSION=1.0.0
|
| 14 |
|
| 15 |
# === DATABASE ===
|
backend/api/v1/routers/auth.py
CHANGED
|
@@ -129,18 +129,23 @@ async def register(
|
|
| 129 |
email=body.email,
|
| 130 |
full_name=body.full_name,
|
| 131 |
hashed_password=hash_password(body.password),
|
| 132 |
-
is_active=not settings.
|
| 133 |
-
is_verified=not settings.
|
| 134 |
)
|
| 135 |
db.add(user)
|
| 136 |
await db.commit()
|
| 137 |
await db.refresh(user)
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
|
|
|
| 141 |
|
| 142 |
logger.info("User registered", extra={"user_id": str(user.id), "email": user.email})
|
| 143 |
-
message =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
return MessageResponse(message=message)
|
| 145 |
|
| 146 |
@router.post("/login", response_model=AuthResponse)
|
|
@@ -159,7 +164,10 @@ async def login(
|
|
| 159 |
await brute_force_protector.check_and_record_failure(client_ip)
|
| 160 |
raise AuthenticationError("Invalid email or password")
|
| 161 |
|
| 162 |
-
if not user.is_active:
|
|
|
|
|
|
|
|
|
|
| 163 |
raise AccountInactiveError("Please verify your email before logging in")
|
| 164 |
|
| 165 |
brute_force_protector.record_success(client_ip)
|
|
|
|
| 129 |
email=body.email,
|
| 130 |
full_name=body.full_name,
|
| 131 |
hashed_password=hash_password(body.password),
|
| 132 |
+
is_active=not settings.REQUIRE_EMAIL_VERIFICATION,
|
| 133 |
+
is_verified=not settings.REQUIRE_EMAIL_VERIFICATION,
|
| 134 |
)
|
| 135 |
db.add(user)
|
| 136 |
await db.commit()
|
| 137 |
await db.refresh(user)
|
| 138 |
|
| 139 |
+
if settings.REQUIRE_EMAIL_VERIFICATION:
|
| 140 |
+
plain_token, _token_hash = generate_verification_token()
|
| 141 |
+
background_tasks.add_task(send_verification_email, user.email, plain_token)
|
| 142 |
|
| 143 |
logger.info("User registered", extra={"user_id": str(user.id), "email": user.email})
|
| 144 |
+
message = (
|
| 145 |
+
"Account created. Check your email to verify."
|
| 146 |
+
if settings.REQUIRE_EMAIL_VERIFICATION
|
| 147 |
+
else "Account created. You can sign in now."
|
| 148 |
+
)
|
| 149 |
return MessageResponse(message=message)
|
| 150 |
|
| 151 |
@router.post("/login", response_model=AuthResponse)
|
|
|
|
| 164 |
await brute_force_protector.check_and_record_failure(client_ip)
|
| 165 |
raise AuthenticationError("Invalid email or password")
|
| 166 |
|
| 167 |
+
if not user.is_active and not settings.REQUIRE_EMAIL_VERIFICATION:
|
| 168 |
+
user.is_active = True
|
| 169 |
+
user.is_verified = True
|
| 170 |
+
elif not user.is_active:
|
| 171 |
raise AccountInactiveError("Please verify your email before logging in")
|
| 172 |
|
| 173 |
brute_force_protector.record_success(client_ip)
|
backend/core/config.py
CHANGED
|
@@ -23,6 +23,7 @@ class Settings(BaseSettings):
|
|
| 23 |
TRUSTED_HOSTS: Any = ["localhost", "127.0.0.1", "*.vercel.app", "*.hf.space"]
|
| 24 |
FRONTEND_URL: str = "http://localhost:3000"
|
| 25 |
BACKEND_URL: str = "http://localhost:8000"
|
|
|
|
| 26 |
|
| 27 |
# === Database ===
|
| 28 |
DATABASE_URL: str = "sqlite+aiosqlite:///./medsight.db"
|
|
|
|
| 23 |
TRUSTED_HOSTS: Any = ["localhost", "127.0.0.1", "*.vercel.app", "*.hf.space"]
|
| 24 |
FRONTEND_URL: str = "http://localhost:3000"
|
| 25 |
BACKEND_URL: str = "http://localhost:8000"
|
| 26 |
+
REQUIRE_EMAIL_VERIFICATION: bool = False
|
| 27 |
|
| 28 |
# === Database ===
|
| 29 |
DATABASE_URL: str = "sqlite+aiosqlite:///./medsight.db"
|