Neah/app/silent-refresh/page.tsx
2025-05-02 12:33:43 +02:00

51 lines
1.6 KiB
TypeScript

"use client";
import { useEffect, useState } from 'react';
import { useSession } from 'next-auth/react';
export default function SilentRefresh() {
const { data: session, status } = useSession();
const [message, setMessage] = useState('Checking authentication...');
useEffect(() => {
// Notify parent window of authentication state
const notifyParent = () => {
try {
if (window.parent && window.parent !== window) {
if (status === 'authenticated' && session) {
window.parent.postMessage({
type: 'SILENT_AUTH_SUCCESS',
session: {
authenticated: true,
userId: session.user.id,
username: session.user.username,
roles: session.user.role
}
}, '*');
setMessage('Authentication successful. You can close this window.');
} else if (status === 'unauthenticated') {
window.parent.postMessage({
type: 'SILENT_AUTH_FAILURE',
error: 'Not authenticated'
}, '*');
setMessage('Not authenticated. You may need to log in again.');
}
}
} catch (e) {
console.error('Error notifying parent window:', e);
setMessage('Error communicating with parent window.');
}
};
if (status !== 'loading') {
notifyParent();
}
}, [session, status]);
// This page is meant to be loaded in an iframe, so keep it minimal
return (
<div style={{ padding: '20px', fontFamily: 'sans-serif', color: '#666' }}>
{message}
</div>
);
}