2025-03-11 13:12:15 +00:00

32 lines
1.1 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from typing import Optional
from models import User
from database import get_db
from utils import send_reset_password_email
router = APIRouter()
@router.post("/forgot-password", status_code=status.HTTP_200_OK)
async def forgot_password(email: str, db: Session = Depends(get_db)):
"""
Endpoint for initiating the forgot password process.
Args:
email (str): The email address of the user who forgot their password.
db (Session, optional): The database session object. Defaults to Depends(get_db).
Raises:
HTTPException: If the email address is not found in the database.
Returns:
dict: A success message indicating that the reset password email has been sent.
"""
user = db.query(User).filter(User.email == email).first()
if not user:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User with this email not found")
# Generate and send reset password email
send_reset_password_email(user.email, user.id)
return {"message": "Reset password email sent successfully"}