Files
geointel/frontend/src/hooks/useOperatorSession.ts
T
Jens c76a746cd7
GeoIntel release gates / Compile, test, contracts and builds (push) Canceled after 0s
GeoIntel release gates / Python and npm vulnerability policy (push) Canceled after 0s
GeoIntel release gates / GIS image, SBOM and container scan (push) Canceled after 0s
Update
2026-07-27 23:28:43 +02:00

71 lines
2.0 KiB
TypeScript

import { useEffect, useState } from 'react'
import { formatAuthError } from '../lib/authError'
import { getAuthSession, logout, type AuthSession } from '../services/api/auth'
const signedOutSession: AuthSession = {
authentication_required: true,
authenticated: false,
username: null,
expires_at: null,
role: null,
guest_access_enabled: false,
guest_project_id: null,
}
export function useOperatorSession() {
const [session, setSession] = useState<AuthSession | null>(null)
const [sessionError, setSessionError] = useState<string | null>(null)
const [loggingOut, setLoggingOut] = useState(false)
useEffect(() => {
let active = true
getAuthSession()
.then((value) => {
if (active) {
setSession(value)
setSessionError(null)
}
})
.catch((error) => {
if (active) {
setSession(signedOutSession)
setSessionError(formatAuthError(error, 'De aanmeldservice is tijdelijk niet bereikbaar. Probeer het over enkele ogenblikken opnieuw.'))
}
})
return () => {
active = false
}
}, [])
useEffect(() => {
const expireSession = () => {
setSession((current) => ({
...signedOutSession,
guest_access_enabled: current?.guest_access_enabled ?? false,
}))
setSessionError('Uw sessie is verlopen. Meld u opnieuw aan.')
}
window.addEventListener('geointel:session-expired', expireSession)
return () => window.removeEventListener('geointel:session-expired', expireSession)
}, [])
const handleLogout = async () => {
setLoggingOut(true)
try {
setSession(await logout())
setSessionError(null)
} catch (error) {
setSessionError(formatAuthError(error, 'Uitloggen is niet gelukt. Vernieuw de pagina en probeer opnieuw.'))
} finally {
setLoggingOut(false)
}
}
const handleAuthenticated = (authenticatedSession: AuthSession) => {
setSession(authenticatedSession)
setSessionError(null)
}
return { session, sessionError, loggingOut, handleAuthenticated, handleLogout }
}