ffg
Browse files
frontend/src/pages/ResetPassword.jsx
CHANGED
|
@@ -86,50 +86,66 @@ const ResetPassword = () => {
|
|
| 86 |
|
| 87 |
const handleSubmit = async (e) => {
|
| 88 |
e.preventDefault();
|
| 89 |
-
|
| 90 |
// Basic validation
|
| 91 |
if (formData.password !== formData.confirmPassword) {
|
| 92 |
alert('Passwords do not match');
|
| 93 |
return;
|
| 94 |
}
|
| 95 |
-
|
| 96 |
if (formData.password.length < 8) {
|
| 97 |
alert('Password must be at least 8 characters long');
|
| 98 |
return;
|
| 99 |
}
|
| 100 |
-
|
| 101 |
try {
|
| 102 |
-
// ---
|
| 103 |
-
//
|
| 104 |
-
//
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
const { data, error } = await supabase.auth.updateUser({
|
| 109 |
password: formData.password
|
| 110 |
});
|
| 111 |
-
|
| 112 |
if (error) {
|
| 113 |
-
// Handle specific Supabase errors
|
| 114 |
console.error('Supabase password reset error:', error);
|
| 115 |
let message = 'Failed to reset password.';
|
| 116 |
if (error.message.toLowerCase().includes('password')) {
|
| 117 |
message = 'Password does not meet requirements. Please use at least 8 characters.';
|
| 118 |
-
} else if (error.message.toLowerCase().includes('session')) {
|
| 119 |
message = 'Password reset session expired. Please request a new reset link.';
|
| 120 |
} else {
|
| 121 |
message = error.message;
|
| 122 |
}
|
| 123 |
-
|
| 124 |
-
alert(message);
|
| 125 |
return;
|
| 126 |
}
|
| 127 |
-
|
| 128 |
-
// If successful
|
| 129 |
console.log('Password updated successfully:', data);
|
| 130 |
alert('Password reset successfully! You can now log in with your new password.');
|
|
|
|
|
|
|
|
|
|
| 131 |
navigate('/login');
|
| 132 |
-
|
| 133 |
} catch (err) {
|
| 134 |
console.error('Unexpected error during password reset:', err);
|
| 135 |
alert('An unexpected error occurred. Please try again.');
|
|
|
|
| 86 |
|
| 87 |
const handleSubmit = async (e) => {
|
| 88 |
e.preventDefault();
|
| 89 |
+
|
| 90 |
// Basic validation
|
| 91 |
if (formData.password !== formData.confirmPassword) {
|
| 92 |
alert('Passwords do not match');
|
| 93 |
return;
|
| 94 |
}
|
|
|
|
| 95 |
if (formData.password.length < 8) {
|
| 96 |
alert('Password must be at least 8 characters long');
|
| 97 |
return;
|
| 98 |
}
|
| 99 |
+
|
| 100 |
try {
|
| 101 |
+
// --- Check for existing session (recovery session) ---
|
| 102 |
+
// It's crucial to verify the session was established by the Supabase client
|
| 103 |
+
// when it processed the URL fragments on app load.
|
| 104 |
+
const { data: { session }, error: sessionError } = await supabase.auth.getSession();
|
| 105 |
+
if (sessionError) {
|
| 106 |
+
console.error('Error getting session:', sessionError);
|
| 107 |
+
alert('Failed to get session. Please try resetting your password again.');
|
| 108 |
+
// Optionally, redirect to /forgot-password
|
| 109 |
+
// navigate('/forgot-password');
|
| 110 |
+
return;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
if (!session || !session.user) {
|
| 114 |
+
// This is the likely cause of AuthSessionMissingError
|
| 115 |
+
console.warn('No active session found. The URL might not have been processed correctly or the session expired.');
|
| 116 |
+
alert('Password reset link may be invalid or expired. Please request a new one.');
|
| 117 |
+
// Optionally, redirect to /forgot-password
|
| 118 |
+
// navigate('/forgot-password');
|
| 119 |
+
return;
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
console.log('Resetting password with Supabase client for user:', session.user?.email);
|
| 123 |
+
|
| 124 |
const { data, error } = await supabase.auth.updateUser({
|
| 125 |
password: formData.password
|
| 126 |
});
|
| 127 |
+
|
| 128 |
if (error) {
|
|
|
|
| 129 |
console.error('Supabase password reset error:', error);
|
| 130 |
let message = 'Failed to reset password.';
|
| 131 |
if (error.message.toLowerCase().includes('password')) {
|
| 132 |
message = 'Password does not meet requirements. Please use at least 8 characters.';
|
| 133 |
+
} else if (error.message.toLowerCase().includes('session') || error.message.includes("Auth session missing")) {
|
| 134 |
message = 'Password reset session expired. Please request a new reset link.';
|
| 135 |
} else {
|
| 136 |
message = error.message;
|
| 137 |
}
|
| 138 |
+
alert(message);
|
|
|
|
| 139 |
return;
|
| 140 |
}
|
| 141 |
+
|
|
|
|
| 142 |
console.log('Password updated successfully:', data);
|
| 143 |
alert('Password reset successfully! You can now log in with your new password.');
|
| 144 |
+
// After successful reset, the session might need to be cleared or re-established.
|
| 145 |
+
// Consider logging the user out of the recovery session.
|
| 146 |
+
await supabase.auth.signOut();
|
| 147 |
navigate('/login');
|
| 148 |
+
|
| 149 |
} catch (err) {
|
| 150 |
console.error('Unexpected error during password reset:', err);
|
| 151 |
alert('An unexpected error occurred. Please try again.');
|
frontend/src/services/supabaseClient.js
CHANGED
|
@@ -38,4 +38,17 @@ export const supabase = createClient(
|
|
| 38 |
}
|
| 39 |
);
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
export default supabase;
|
|
|
|
| 38 |
}
|
| 39 |
);
|
| 40 |
|
| 41 |
+
// --- Add logging to debug session initialization ---
|
| 42 |
+
console.log("Supabase client initialized with URL:", supabaseUrl);
|
| 43 |
+
supabase.auth.getSession()
|
| 44 |
+
.then(({ data: { session }, error }) => {
|
| 45 |
+
if (error) {
|
| 46 |
+
console.error("Error getting initial session:", error);
|
| 47 |
+
} else {
|
| 48 |
+
console.log("Initial session from URL (on app load):", session ? "Session exists" : "No session");
|
| 49 |
+
// Note: The actual session details might be sensitive, so just log existence.
|
| 50 |
+
// If session is null here when coming from a reset link, it means processing failed.
|
| 51 |
+
}
|
| 52 |
+
});
|
| 53 |
+
|
| 54 |
export default supabase;
|