Skip to content

41 make sure we correctly handle password reset from logged in users #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,7 @@ async def read_forgot_password(
params: dict = Depends(common_unauthenticated_parameters),
show_form: Optional[str] = "true",
):
if params["user"]:
return RedirectResponse(url="/dashboard", status_code=302)
params["show_form"] = show_form
params["show_form"] = show_form == "true"

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

Expand Down
13 changes: 11 additions & 2 deletions routers/authentication.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# auth.py
from logging import getLogger
from typing import Optional
from urllib.parse import urlparse
from datetime import datetime
from fastapi import APIRouter, Depends, HTTPException, BackgroundTasks, Form
from fastapi import APIRouter, Depends, HTTPException, BackgroundTasks, Form, Request
from fastapi.responses import RedirectResponse
from pydantic import BaseModel, EmailStr, ConfigDict
from sqlmodel import Session, select
Expand Down Expand Up @@ -300,6 +301,7 @@ async def refresh_token(
@router.post("/forgot_password")
async def forgot_password(
background_tasks: BackgroundTasks,
request: Request,
user: UserForgotPassword = Depends(UserForgotPassword.as_form),
session: Session = Depends(get_session)
):
Expand All @@ -309,7 +311,14 @@ async def forgot_password(
if db_user:
background_tasks.add_task(send_reset_email, user.email, session)

return RedirectResponse(url="/forgot_password?show_form=false", status_code=303)
# Get the referer header, default to /forgot_password if not present
referer = request.headers.get("referer", "/forgot_password")

# Extract the path from the full URL
redirect_path = urlparse(referer).path

# Add the query parameter to the redirect path
return RedirectResponse(url=f"{redirect_path}?show_form=false", status_code=303)


@router.post("/reset_password")
Expand Down
4 changes: 4 additions & 0 deletions templates/users/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,15 @@ <h1 class="mb-4">User Profile</h1>
Change Password
</div>
<div class="card-body">
{% if show_form %}
<form action="{{ url_for('forgot_password') }}" method="post">
<input type="hidden" name="email" value="{{ user.email }}">
<p>To change your password, please confirm your email. A password reset link will be sent to your email address.</p>
<button type="submit" class="btn btn-primary">Send Password Reset Email</button>
</form>
{% else %}
<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>
{% endif %}
</div>
</div>

Expand Down
Loading