2026-04-04 21:48:56 +02:00
|
|
|
import { useEffect } from 'react';
|
|
|
|
|
import { useNavigate } from 'react-router';
|
|
|
|
|
import { useAuthStore } from './auth-store';
|
|
|
|
|
import { Spinner } from '@cameleer/design-system';
|
2026-04-05 00:22:22 +02:00
|
|
|
import { fetchConfig } from '../config';
|
2026-04-05 00:48:21 +02:00
|
|
|
import { getCodeVerifier } from './pkce';
|
2026-04-04 21:48:56 +02:00
|
|
|
|
|
|
|
|
export function CallbackPage() {
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const login = useAuthStore((s) => s.login);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const params = new URLSearchParams(window.location.search);
|
|
|
|
|
const code = params.get('code');
|
|
|
|
|
if (!code) {
|
|
|
|
|
navigate('/login');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 00:48:21 +02:00
|
|
|
const codeVerifier = getCodeVerifier();
|
|
|
|
|
if (!codeVerifier) {
|
|
|
|
|
navigate('/login');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 21:48:56 +02:00
|
|
|
const redirectUri = `${window.location.origin}/callback`;
|
|
|
|
|
|
2026-04-05 00:22:22 +02:00
|
|
|
fetchConfig().then((config) => {
|
|
|
|
|
fetch(`${config.logtoEndpoint}/oidc/token`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
|
|
body: new URLSearchParams({
|
|
|
|
|
grant_type: 'authorization_code',
|
|
|
|
|
code,
|
|
|
|
|
client_id: config.logtoClientId,
|
|
|
|
|
redirect_uri: redirectUri,
|
2026-04-05 00:48:21 +02:00
|
|
|
code_verifier: codeVerifier,
|
2026-04-05 00:22:22 +02:00
|
|
|
}),
|
2026-04-04 21:48:56 +02:00
|
|
|
})
|
2026-04-05 00:22:22 +02:00
|
|
|
.then((r) => r.json())
|
|
|
|
|
.then((data) => {
|
|
|
|
|
if (data.access_token) {
|
|
|
|
|
login(data.access_token, data.refresh_token || '');
|
|
|
|
|
navigate('/');
|
|
|
|
|
} else {
|
|
|
|
|
navigate('/login');
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(() => navigate('/login'));
|
|
|
|
|
});
|
2026-04-04 21:48:56 +02:00
|
|
|
}, [login, navigate]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', minHeight: '100vh' }}>
|
|
|
|
|
<Spinner />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|