Skip to content

Commit 10db84d

Browse files
Merge pull request #83 from Promptly-Technologies-LLC/41-make-sure-we-correctly-handle-password-reset-from-logged-in-users
41 make sure we correctly handle password reset from logged in users
2 parents 6d7f509 + 5531687 commit 10db84d

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

main.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,7 @@ async def read_forgot_password(
194194
params: dict = Depends(common_unauthenticated_parameters),
195195
show_form: Optional[str] = "true",
196196
):
197-
if params["user"]:
198-
return RedirectResponse(url="/dashboard", status_code=302)
199-
params["show_form"] = show_form
197+
params["show_form"] = show_form == "true"
200198

201199
return templates.TemplateResponse(params["request"], "authentication/forgot_password.html", params)
202200

routers/authentication.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# auth.py
22
from logging import getLogger
33
from typing import Optional
4+
from urllib.parse import urlparse
45
from datetime import datetime
5-
from fastapi import APIRouter, Depends, HTTPException, BackgroundTasks, Form
6+
from fastapi import APIRouter, Depends, HTTPException, BackgroundTasks, Form, Request
67
from fastapi.responses import RedirectResponse
78
from pydantic import BaseModel, EmailStr, ConfigDict
89
from sqlmodel import Session, select
@@ -300,6 +301,7 @@ async def refresh_token(
300301
@router.post("/forgot_password")
301302
async def forgot_password(
302303
background_tasks: BackgroundTasks,
304+
request: Request,
303305
user: UserForgotPassword = Depends(UserForgotPassword.as_form),
304306
session: Session = Depends(get_session)
305307
):
@@ -309,7 +311,14 @@ async def forgot_password(
309311
if db_user:
310312
background_tasks.add_task(send_reset_email, user.email, session)
311313

312-
return RedirectResponse(url="/forgot_password?show_form=false", status_code=303)
314+
# Get the referer header, default to /forgot_password if not present
315+
referer = request.headers.get("referer", "/forgot_password")
316+
317+
# Extract the path from the full URL
318+
redirect_path = urlparse(referer).path
319+
320+
# Add the query parameter to the redirect path
321+
return RedirectResponse(url=f"{redirect_path}?show_form=false", status_code=303)
313322

314323

315324
@router.post("/reset_password")

templates/users/profile.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,15 @@ <h1 class="mb-4">User Profile</h1>
9393
Change Password
9494
</div>
9595
<div class="card-body">
96+
{% if show_form %}
9697
<form action="{{ url_for('forgot_password') }}" method="post">
9798
<input type="hidden" name="email" value="{{ user.email }}">
9899
<p>To change your password, please confirm your email. A password reset link will be sent to your email address.</p>
99100
<button type="submit" class="btn btn-primary">Send Password Reset Email</button>
100101
</form>
102+
{% else %}
103+
<p>A password reset link has been sent to your email address. Note that you can request a password reset only once an hour. If you have not received an email, please check your spam folder or try again later.</p>
104+
{% endif %}
101105
</div>
102106
</div>
103107

0 commit comments

Comments
 (0)