-
Notifications
You must be signed in to change notification settings - Fork 40
feat: Add secure email change flow with verification to OLD email (#407) #446
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
Open
Adil2009700
wants to merge
5
commits into
main
Choose a base branch
from
fix/secure-email-change-407
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3ef215b
feat: Student rework view - show exactly what failed during QA review…
4f115bd
fix: Add error handling to QA queue approve/reject functions (#416) -…
f9730f2
fix: Move XP reward inside transaction to prevent permanent XP loss (…
badef28
fix: Move XP reward processing inside transaction to prevent permanen…
3d1dbe2
feat: Add secure email change flow with verification to OLD email (#407)
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,231 @@ | ||
| import { NextResponse } from 'next/server'; | ||
| import { prisma } from '@/lib/db'; | ||
| import crypto from 'crypto'; | ||
|
|
||
| export async function GET(req: Request) { | ||
| try { | ||
| const url = new URL(req.url); | ||
| const token = url.searchParams.get('token'); | ||
|
|
||
| if (!token) { | ||
| return NextResponse.json({ error: 'Token is required' }, { status: 400 }); | ||
| } | ||
|
|
||
| // Find the email change request | ||
| const emailChangeRequest = await prisma.emailChangeRequest.findUnique({ | ||
| where: { token }, | ||
| include: { user: true }, | ||
| }); | ||
|
|
||
| if (!emailChangeRequest) { | ||
| return NextResponse.json({ error: 'Invalid or expired token' }, { status: 404 }); | ||
| } | ||
|
|
||
| // Check if token is already used | ||
| if (emailChangeRequest.status === 'confirmed') { | ||
| return NextResponse.json({ error: 'This token has already been used' }, { status: 400 }); | ||
| } | ||
|
|
||
| // Check if token is expired | ||
| if (emailChangeRequest.expiresAt < new Date()) { | ||
| // Update status to expired | ||
| await prisma.emailChangeRequest.update({ | ||
| where: { id: emailChangeRequest.id }, | ||
| data: { status: 'expired' }, | ||
| }); | ||
| return NextResponse.json({ error: 'Token has expired' }, { status: 400 }); | ||
| } | ||
|
|
||
| // Check if new email is still available (another user might have registered with it) | ||
| const existingUser = await prisma.user.findUnique({ | ||
| where: { email: emailChangeRequest.newEmail }, | ||
| }); | ||
|
|
||
| if (existingUser && existingUser.id !== emailChangeRequest.userId) { | ||
| return NextResponse.json( | ||
| { error: 'This email is now in use by another account. Please request a new email change.' }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| // Use transaction to ensure atomic update | ||
| await prisma.$transaction(async (tx) => { | ||
| // Update user's email | ||
| await tx.user.update({ | ||
| where: { id: emailChangeRequest.userId }, | ||
| data: { | ||
| email: emailChangeRequest.newEmail, | ||
| // Force session invalidation by updating updatedAt timestamp | ||
| updatedAt: new Date(), | ||
| }, | ||
| }); | ||
|
|
||
| // Mark email change request as confirmed | ||
| await tx.emailChangeRequest.update({ | ||
| where: { id: emailChangeRequest.id }, | ||
| data: { status: 'confirmed' }, | ||
| }); | ||
|
|
||
| // Invalidate any other pending email change requests for this user | ||
| await tx.emailChangeRequest.updateMany({ | ||
| where: { | ||
| userId: emailChangeRequest.userId, | ||
| id: { not: emailChangeRequest.id }, | ||
| status: 'pending', | ||
| }, | ||
| data: { status: 'expired' }, | ||
| }); | ||
| }); | ||
|
|
||
| // Return success page or redirect to login | ||
| const html = ` | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Email Changed Successfully</title> | ||
| <style> | ||
| body { | ||
| font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif; | ||
| display: flex; | ||
| justify-content: center; | ||
| align-items: center; | ||
| min-height: 100vh; | ||
| margin: 0; | ||
| background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); | ||
| } | ||
| .container { | ||
| background: white; | ||
| padding: 2rem; | ||
| border-radius: 12px; | ||
| box-shadow: 0 20px 60px rgba(0,0,0,0.3); | ||
| text-align: center; | ||
| max-width: 400px; | ||
| } | ||
| .success-icon { | ||
| font-size: 48px; | ||
| color: #10b981; | ||
| margin-bottom: 1rem; | ||
| } | ||
| h1 { | ||
| color: #1f2937; | ||
| margin-bottom: 1rem; | ||
| } | ||
| p { | ||
| color: #6b7280; | ||
| margin-bottom: 2rem; | ||
| line-height: 1.5; | ||
| } | ||
| .login-button { | ||
| background: #667eea; | ||
| color: white; | ||
| border: none; | ||
| padding: 12px 24px; | ||
| border-radius: 6px; | ||
| font-size: 16px; | ||
| cursor: pointer; | ||
| text-decoration: none; | ||
| display: inline-block; | ||
| } | ||
| .login-button:hover { | ||
| background: #5a67d8; | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <div class="container"> | ||
| <div class="success-icon">✓</div> | ||
| <h1>Email Changed Successfully!</h1> | ||
| <p>Your email has been updated to <strong>${emailChangeRequest.newEmail}</strong>.</p> | ||
| <p>For security reasons, you have been logged out. Please log in again with your new email address.</p> | ||
| <a href="/login" class="login-button">Go to Login</a> | ||
| </div> | ||
| </body> | ||
| </html> | ||
| `; | ||
|
|
||
| return new NextResponse(html, { | ||
| status: 200, | ||
| headers: { | ||
| 'Content-Type': 'text/html', | ||
| }, | ||
| }); | ||
|
|
||
| } catch (error) { | ||
| console.error('Failed to confirm email change:', error); | ||
|
|
||
| const html = ` | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Error Confirming Email Change</title> | ||
| <style> | ||
| body { | ||
| font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif; | ||
| display: flex; | ||
| justify-content: center; | ||
| align-items: center; | ||
| min-height: 100vh; | ||
| margin: 0; | ||
| background: linear-gradient(135deg, #f56565 0%, #ed8936 100%); | ||
| } | ||
| .container { | ||
| background: white; | ||
| padding: 2rem; | ||
| border-radius: 12px; | ||
| box-shadow: 0 20px 60px rgba(0,0,0,0.3); | ||
| text-align: center; | ||
| max-width: 400px; | ||
| } | ||
| .error-icon { | ||
| font-size: 48px; | ||
| color: #f56565; | ||
| margin-bottom: 1rem; | ||
| } | ||
| h1 { | ||
| color: #1f2937; | ||
| margin-bottom: 1rem; | ||
| } | ||
| p { | ||
| color: #6b7280; | ||
| margin-bottom: 2rem; | ||
| line-height: 1.5; | ||
| } | ||
| .home-button { | ||
| background: #f56565; | ||
| color: white; | ||
| border: none; | ||
| padding: 12px 24px; | ||
| border-radius: 6px; | ||
| font-size: 16px; | ||
| cursor: pointer; | ||
| text-decoration: none; | ||
| display: inline-block; | ||
| } | ||
| .home-button:hover { | ||
| background: #e53e3e; | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <div class="container"> | ||
| <div class="error-icon">✗</div> | ||
| <h1>Error Confirming Email Change</h1> | ||
| <p>There was an error processing your email change request. Please try requesting a new email change.</p> | ||
| <a href="/dashboard/settings" class="home-button">Go to Settings</a> | ||
| </div> | ||
| </body> | ||
| </html> | ||
| `; | ||
|
|
||
| return new NextResponse(html, { | ||
| status: 500, | ||
| headers: { | ||
| 'Content-Type': 'text/html', | ||
| }, | ||
| }); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a
GEThandler that mutates state (changes the user's email, marks tokens confirmed/expired). Once real email delivery is wired up (see the other comment), email link-scanners, antivirus products, and browser prefetching all commonly auto-visitGETlinks found in emails — which would silently trigger this confirmation without the user ever clicking. Recommend making the actual state change aPOSTfrom a confirmation page theGETrenders (GET shows "click to confirm", POST does the mutation), which is the standard pattern for exactly this kind of link-triggered mutation.