|
1 | 1 | # auth.py
|
2 | 2 | from typing import Optional
|
3 | 3 | from datetime import datetime
|
4 |
| -from fastapi import APIRouter, Depends, HTTPException, Form |
| 4 | +from fastapi import APIRouter, Depends, HTTPException, Form, BackgroundTasks |
5 | 5 | from fastapi.responses import RedirectResponse
|
| 6 | +# from sendgrid import SendGridAPIClient |
| 7 | +# from sendgrid.helpers.mail import Mail |
6 | 8 | from pydantic import BaseModel, EmailStr, ConfigDict
|
7 | 9 | from sqlmodel import Session, select
|
8 | 10 | from utils.db import User
|
@@ -151,21 +153,53 @@ def refresh_token(
|
151 | 153 | return response
|
152 | 154 |
|
153 | 155 |
|
| 156 | +class EmailSchema(BaseModel): |
| 157 | + email: EmailStr |
| 158 | + |
| 159 | + |
| 160 | +class ResetSchema(BaseModel): |
| 161 | + token: str |
| 162 | + new_password: str |
| 163 | + |
| 164 | + |
154 | 165 | @router.post("/forgot_password")
|
155 | 166 | def forgot_password(user: UserCreate, session: Session = Depends(get_session)):
|
156 | 167 | # db_user = session.exec(select(User).where(
|
157 | 168 | # User.email == user.email)).first()
|
158 | 169 | # 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."} |
162 | 181 |
|
163 | 182 |
|
164 | 183 | @router.post("/reset_password")
|
165 | 184 | def reset_password(
|
166 | 185 | token: str, new_password: str, session: Session = Depends(get_session)
|
167 | 186 | ):
|
168 | 187 | # 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] |
169 | 203 | return {"msg": "Password reset successfully"}
|
170 | 204 |
|
171 | 205 |
|
|
0 commit comments