| 34 | }, [currentEmail, navigate, toast]); |
| 35 | |
| 36 | const handleSubmit = async (e) => { |
| 37 | e.preventDefault(); |
| 38 | |
| 39 | if (!newEmail || !newEmail.includes('@')) { |
| 40 | toast({ |
| 41 | variant: "destructive", |
| 42 | title: "Invalid Email", |
| 43 | description: "Please enter a valid email address.", |
| 44 | }); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | if (newEmail.toLowerCase() === currentEmail.toLowerCase()) { |
| 49 | toast({ |
| 50 | variant: "destructive", |
| 51 | title: "Same Email", |
| 52 | description: "Please enter a different email address.", |
| 53 | }); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | if (newEmail.includes('noreply')) { |
| 58 | toast({ |
| 59 | variant: "destructive", |
| 60 | title: "Invalid Email", |
| 61 | description: "Please use a valid personal email address, not a noreply address.", |
| 62 | }); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | setIsLoading(true); |
| 67 | |
| 68 | try { |
| 69 | await subdomainAPI.post('/auth/email/change-email', { newEmail }); |
| 70 | |
| 71 | // If we got here without error, email change was successful |
| 72 | toast({ |
| 73 | title: "Email Updated", |
| 74 | description: "Redirecting to verification...", |
| 75 | }); |
| 76 | |
| 77 | // Redirect to verification page with the new email |
| 78 | setTimeout(() => { |
| 79 | window.location.href = `/verify-email?email=${encodeURIComponent(newEmail)}`; |
| 80 | }, 1000); |
| 81 | |
| 82 | } catch (err) { |
| 83 | console.error("Email change failed", err); |
| 84 | |
| 85 | // Handle GitHub re-authentication requirement |
| 86 | if (err.data?.error === 'github_reauth_required') { |
| 87 | toast({ |
| 88 | variant: "destructive", |
| 89 | title: "Re-authentication Required", |
| 90 | description: "Please log in with GitHub again to change your email.", |
| 91 | }); |
| 92 | setTimeout(() => { |
| 93 | window.location.href = `${import.meta.env.VITE_API_URL || 'http://localhost:5000'}/auth/github`; |