Skip to content

Commit 4e0f364

Browse files
Log user in with new email when updating email
1 parent 2d46a3d commit 4e0f364

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

routers/authentication.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -357,19 +357,31 @@ async def confirm_email_update(
357357
if not user or not update_token:
358358
raise AuthenticationError()
359359

360-
# Get the new email from the most recent unconfirmed token
361-
if update_token.is_expired():
362-
raise HTTPException(
363-
status_code=400,
364-
detail="Token has expired"
365-
)
366-
367360
# Update email and mark token as used
368361
user.email = new_email
369362
update_token.used = True
370363
session.commit()
371364

372-
return RedirectResponse(
373-
url="/login?email_updated=true",
365+
# Create new tokens with the updated email
366+
access_token = create_access_token(data={"sub": new_email})
367+
refresh_token = create_refresh_token(data={"sub": new_email})
368+
369+
response = RedirectResponse(
370+
url="/profile?email_updated=true",
374371
status_code=303
375372
)
373+
response.set_cookie(
374+
key="access_token",
375+
value=access_token,
376+
httponly=True,
377+
secure=True,
378+
samesite="strict"
379+
)
380+
response.set_cookie(
381+
key="refresh_token",
382+
value=refresh_token,
383+
httponly=True,
384+
secure=True,
385+
samesite="strict"
386+
)
387+
return response

templates/authentication/login.html

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@
66

77
{% block auth_content %}
88
<div class="login-form">
9-
10-
{% if email_updated == "true" %}
11-
<div class="alert alert-success" role="alert">
12-
Your email address has been successfully updated. Please login with your new email address.
13-
</div>
14-
{% endif %}
15-
169
<form method="POST" action="{{ url_for('login') }}" class="needs-validation" novalidate>
1710
<!-- Email Input -->
1811
<div class="mb-3">

templates/users/profile.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ <h1 class="mb-4">User Profile</h1>
1414
</div>
1515
{% endif %}
1616

17+
{% if email_updated == "true" %}
18+
<div class="alert alert-success" role="alert">
19+
Your email address has been successfully updated.
20+
</div>
21+
{% endif %}
22+
1723
<!-- Basic Information -->
1824
<div class="card mb-4" id="basic-info">
1925
<div class="card-header">

0 commit comments

Comments
 (0)