Skip to content

Commit a4b2223

Browse files
Added some commented boilerplate related to password reset flow
1 parent 52f365e commit a4b2223

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

routers/auth.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# auth.py
22
from typing import Optional
33
from datetime import datetime
4-
from fastapi import APIRouter, Depends, HTTPException, Form
4+
from fastapi import APIRouter, Depends, HTTPException, Form, BackgroundTasks
55
from fastapi.responses import RedirectResponse
6+
# from sendgrid import SendGridAPIClient
7+
# from sendgrid.helpers.mail import Mail
68
from pydantic import BaseModel, EmailStr, ConfigDict
79
from sqlmodel import Session, select
810
from utils.db import User
@@ -151,21 +153,53 @@ def refresh_token(
151153
return response
152154

153155

156+
class EmailSchema(BaseModel):
157+
email: EmailStr
158+
159+
160+
class ResetSchema(BaseModel):
161+
token: str
162+
new_password: str
163+
164+
154165
@router.post("/forgot_password")
155166
def forgot_password(user: UserCreate, session: Session = Depends(get_session)):
156167
# db_user = session.exec(select(User).where(
157168
# User.email == user.email)).first()
158169
# TODO: Send reset password email
159-
return {
160-
"msg": "If an account with that email exists, a password reset link has been sent."
161-
}
170+
# email = email_schema.email
171+
# if email not in user_store:
172+
# raise HTTPException(status_code=404, detail="User not found")
173+
174+
# token = str(uuid.uuid4())
175+
# expiration = datetime.utcnow() + timedelta(hours=1)
176+
# token_store[token] = {"email": email, "expiration": expiration}
177+
178+
# background_tasks.add_task(send_reset_email, email, token)
179+
180+
return {"message": "If an account exists with this email, a password reset link will be sent."}
162181

163182

164183
@router.post("/reset_password")
165184
def reset_password(
166185
token: str, new_password: str, session: Session = Depends(get_session)
167186
):
168187
# TODO: Reset password
188+
# token = reset_schema.token
189+
# new_password = reset_schema.new_password
190+
191+
# if token not in token_store:
192+
# raise HTTPException(status_code=400, detail="Invalid or expired token")
193+
194+
# token_data = token_store[token]
195+
# if datetime.utcnow() > token_data['expiration']:
196+
# del token_store[token]
197+
# raise HTTPException(status_code=400, detail="Token has expired")
198+
199+
# # Update password (replace with actual password update logic)
200+
# # user_store[token_data['email']]['password'] = hash_password(new_password)
201+
202+
# del token_store[token]
169203
return {"msg": "Password reset successfully"}
170204

171205

utils/auth.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import logging
55
from dotenv import load_dotenv
66
from sqlmodel import Session, select
7-
from sqlalchemy.engine import URL
7+
# from sendgrid import SendGridAPIClient
8+
# from sendgrid.helpers.mail import Mail
89
from passlib.context import CryptContext
910
from datetime import UTC, datetime, timedelta
1011
from typing import Optional
@@ -168,3 +169,17 @@ def __init__(self, user: User, access_token: str, refresh_token: str):
168169
self.user = user
169170
self.access_token = access_token
170171
self.refresh_token = refresh_token
172+
173+
174+
# def send_reset_email(email: str, token: str):
175+
# message = Mail(
176+
# from_email="[email protected]",
177+
# to_emails=email,
178+
# subject="Password Reset Request",
179+
# html_content=f'<p>Click <a href="https://yourdomain.com/reset?token={token}">here</a> to reset your password.</p>'
180+
# )
181+
# try:
182+
# sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
183+
# sg.send(message)
184+
# except Exception as e:
185+
# print(f"Error sending email: {e}")

0 commit comments

Comments
 (0)