Files
DocNest/frontend/src/pages/AuthCallback.tsx
M1ngdaXie 763575f284 fix: restore original URL after OAuth login redirect
Save the intended destination to sessionStorage before navigating to
the OAuth provider, and read it back in AuthCallback after login.
Also handles 401-triggered redirects so session-expired users are
returned to the page they were on.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-09 04:42:19 +00:00

73 lines
2.0 KiB
TypeScript

import { useEffect, useState } from 'react';
import { useNavigate, useSearchParams } from 'react-router-dom';
import { useAuth } from '../contexts/AuthContext';
function AuthCallback() {
const [searchParams] = useSearchParams();
const navigate = useNavigate();
const { login } = useAuth();
const [error, setError] = useState<string | null>(null);
useEffect(() => {
const handleCallback = async () => {
const token = searchParams.get('token');
const redirect =
searchParams.get('redirect') ||
sessionStorage.getItem('oauth_redirect') ||
'/';
sessionStorage.removeItem('oauth_redirect');
if (!token) {
setError('No authentication token received');
setTimeout(() => navigate('/login'), 2000);
return;
}
try {
await login(token);
navigate(redirect);
} catch (err) {
console.error('Login error:', err);
setError('Authentication failed. Please try again.');
setTimeout(() => navigate('/login'), 2000);
}
};
handleCallback();
}, [searchParams, login, navigate]);
return (
<div style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
minHeight: '100vh',
background: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
}}>
<div style={{
background: 'white',
borderRadius: '16px',
padding: '48px',
textAlign: 'center',
}}>
{error ? (
<>
<h2 style={{ color: '#e53e3e', marginBottom: '8px' }}>Error</h2>
<p style={{ color: '#718096' }}>{error}</p>
<p style={{ color: '#718096', fontSize: '14px', marginTop: '16px' }}>
Redirecting to login...
</p>
</>
) : (
<>
<h2 style={{ color: '#1a202c', marginBottom: '8px' }}>Logging you in...</h2>
<p style={{ color: '#718096' }}>Please wait while we complete the authentication.</p>
</>
)}
</div>
</div>
);
}
export default AuthCallback;