| from datetime import datetime, timedelta, timezone | |
| from typing import Optional | |
| from jose import jwt | |
| from passlib.context import CryptContext | |
| from database import get_users_collection | |
| import uuid | |
| import httpx | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| SECRET_KEY = os.getenv("SECRET_KEY") | |
| ALGORITHM = "HS256" | |
| ACCESS_TOKEN_EXPIRE_MINUTES = 30 | |
| pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") | |
| def verify_password(plain_password: str, hashed_password: str) -> bool: | |
| return pwd_context.verify(plain_password, hashed_password) | |
| def get_password_hash(password: str) -> str: | |
| return pwd_context.hash(password) | |
| def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str: | |
| to_encode = data.copy() | |
| expire = datetime.now(timezone.utc) + (expires_delta or timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)) | |
| to_encode.update({"exp": expire}) | |
| return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM) | |
| async def get_user(email: str): | |
| users_collection = await get_users_collection() | |
| user = await users_collection.find_one({"email": email}) | |
| return user | |
| def create_verification_token(email: str) -> str: | |
| return str(uuid.uuid4()) | |
| async def send_verification_email(to_email: str, token: str): | |
| async with httpx.AsyncClient() as client: | |
| try: | |
| response = await client.post( | |
| "https://email-verification-texttrace.vercel.app/send-email", | |
| json={"email": to_email, "token": token} | |
| ) | |
| response.raise_for_status() | |
| except httpx.HTTPError as e: | |
| print(f"Error sending email: {e}") | |