# app/modules/tenancy/routes/pages/email_verification.py
"""
Email verification page route.
Renders HTML result pages for email verification:
- GET /verify-email?token={token} - Verify email and show result page
"""
import logging
from fastapi import APIRouter, Depends, Query, Request
from fastapi.responses import HTMLResponse
from sqlalchemy.orm import Session
from app.core.database import get_db
from app.modules.tenancy.models.email_verification_token import EmailVerificationToken
router = APIRouter()
logger = logging.getLogger(__name__)
ROUTE_CONFIG = {
"prefix": "",
}
# Shared HTML template for verification result
_HTML_TEMPLATE = """
"""
@router.get("/verify-email", response_class=HTMLResponse, include_in_schema=False)
def verify_email_page(
request: Request,
token: str = Query(..., description="Email verification token"),
db: Session = Depends(get_db),
):
"""
Verify email address via token link from email.
Validates the token, marks user's email as verified, and renders
a success or error HTML page.
"""
token_record = EmailVerificationToken.find_valid_token(db, token)
if not token_record:
logger.warning("Invalid or expired email verification token used")
return HTMLResponse(
content=_HTML_TEMPLATE.format(
title="Verification Failed",
color_from="#ef4444",
color_to="#dc2626",
icon="❌",
message="This verification link is invalid or has expired. "
"Please request a new verification email.",
link_url="/merchants/login",
link_text="Go to Login",
),
status_code=400,
)
# Mark token as used and verify user's email
user = token_record.user
user.is_email_verified = True
token_record.mark_used(db)
db.commit()
logger.info(f"Email verified for user {user.id} ({user.email})")
return HTMLResponse(
content=_HTML_TEMPLATE.format(
title="Email Verified",
color_from="#10b981",
color_to="#059669",
icon="✅",
message="Your email address has been successfully verified! "
"You can now log in to your account.",
link_url="/merchants/login",
link_text="Go to Login",
)
)