| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import { useEffect } from 'react'; |
| import { useAgentStore } from '@/store/agentStore'; |
| import { logger } from '@/utils/logger'; |
|
|
| |
| export function isInIframe(): boolean { |
| try { |
| return window.top !== window.self; |
| } catch { |
| return true; |
| } |
| } |
|
|
| |
| export function triggerLogin(): void { |
| window.location.href = '/auth/login'; |
| } |
|
|
| |
| |
| |
| |
| export function useAuth() { |
| const setUser = useAgentStore((s) => s.setUser); |
|
|
| useEffect(() => { |
| let cancelled = false; |
|
|
| async function checkAuth() { |
| try { |
| |
| const response = await fetch('/auth/me', { credentials: 'include' }); |
| if (response.ok) { |
| const data = await response.json(); |
| if (!cancelled && data.authenticated) { |
| setUser({ |
| authenticated: true, |
| username: data.username, |
| name: data.name, |
| picture: data.picture, |
| }); |
| logger.log('Authenticated as', data.username); |
| return; |
| } |
| } |
|
|
| |
| const statusRes = await fetch('/auth/status', { credentials: 'include' }); |
| const statusData = await statusRes.json(); |
| if (!statusData.auth_enabled) { |
| |
| if (!cancelled) setUser({ authenticated: true, username: 'dev' }); |
| return; |
| } |
|
|
| |
| if (!cancelled) setUser(null); |
| } catch { |
| |
| if (!cancelled) setUser({ authenticated: true, username: 'dev' }); |
| } |
| } |
|
|
| checkAuth(); |
| return () => { cancelled = true; }; |
| }, [setUser]); |
| } |
|
|