···
5
5
6
6
const LoginCallback = () => {
7
7
// Get loading and authentication status from AuthContext
8
8
-
const { loading, isAuthenticated, error: authError } = useAuth();
8
8
+
const { loading, isAuthenticated, error: authError, session } = useAuth();
9
9
const navigate = useNavigate();
10
10
const location = useLocation();
11
11
const [localError, setLocalError] = useState(null);
12
12
13
13
useEffect(() => {
14
14
+
// Log location information for debugging
15
15
+
console.log('(LoginCallback) Current location:', {
16
16
+
pathname: location.pathname,
17
17
+
search: location.search,
18
18
+
hash: location.hash,
19
19
+
state: location.state
20
20
+
});
21
21
+
14
22
// Don't do anything until the AuthProvider is done loading
15
23
if (loading) {
16
24
return;
···
20
28
if (isAuthenticated) {
21
29
console.log('(LoginCallback) Authentication successful, redirecting...');
22
30
23
23
-
// Parse returnUrl from query parameters
31
31
+
// First, try to get returnUrl from localStorage (set during login)
32
32
+
let returnUrl = localStorage.getItem('auth_redirect_url');
33
33
+
console.log('(LoginCallback) Found returnUrl in localStorage:', returnUrl);
34
34
+
35
35
+
// Parse returnUrl from query parameters (if present)
24
36
const searchParams = new URLSearchParams(location.search);
25
25
-
const returnUrl = searchParams.get('returnUrl') || '/verifier';
37
37
+
if (searchParams.has('returnUrl')) {
38
38
+
returnUrl = searchParams.get('returnUrl');
39
39
+
console.log(`(LoginCallback) Overriding with returnUrl from URL:`, returnUrl);
40
40
+
}
26
41
27
27
-
console.log(`(LoginCallback) Redirecting to: ${returnUrl}`);
28
28
-
navigate(returnUrl, { replace: true }); // Use replace to avoid callback in history
42
42
+
// Check if we have state data from the OAuth callback
43
43
+
if (session?.state) {
44
44
+
try {
45
45
+
// The state is stored as a JSON string
46
46
+
const stateData = JSON.parse(session.state);
47
47
+
if (stateData && stateData.returnUrl) {
48
48
+
console.log(`(LoginCallback) Found returnUrl in OAuth state:`, stateData.returnUrl);
49
49
+
returnUrl = stateData.returnUrl;
50
50
+
}
51
51
+
} catch (err) {
52
52
+
console.error('(LoginCallback) Error parsing state data:', err);
53
53
+
}
54
54
+
}
55
55
+
56
56
+
// Fallback to /verifier if no returnUrl found
57
57
+
if (!returnUrl) {
58
58
+
returnUrl = '/verifier';
59
59
+
console.log(`(LoginCallback) No returnUrl found, defaulting to:`, returnUrl);
60
60
+
}
61
61
+
62
62
+
// Clean up localStorage
63
63
+
localStorage.removeItem('auth_redirect_url');
64
64
+
65
65
+
console.log(`(LoginCallback) Final redirect destination:`, returnUrl);
66
66
+
navigate(returnUrl, { replace: true });
29
67
} else {
30
68
// If not authenticated after loading, something went wrong
31
69
console.error('(LoginCallback) Authentication failed after loading.');
32
70
setLocalError(authError || 'Authentication failed. Please try logging in again.');
33
71
}
34
72
35
35
-
}, [loading, isAuthenticated, navigate, authError, location]); // Added location dependency
73
73
+
}, [loading, isAuthenticated, navigate, authError, location, session]); // Added session dependency
36
74
37
75
// Display loading message
38
76
if (loading) {