2026-09-19 14:17:27 +02:00
|
|
|
import { createContext, useCallback, useContext, useEffect, useMemo, useState, type ReactNode } from 'react'
|
2026-09-19 17:33:44 +02:00
|
|
|
import { apiConfig, requestSession, signInWithGoogle, sudoLogin } from './api'
|
2026-09-19 14:17:27 +02:00
|
|
|
|
2026-09-19 16:45:24 +02:00
|
|
|
// There is no sign-in or sign-out: the upstream SSO proxy decides whether the
|
2026-09-19 16:51:24 +02:00
|
|
|
// current request carries the owner's identity. Signing in just sends the
|
|
|
|
|
// browser to the configured Google/IdP URL.
|
2026-09-19 14:17:27 +02:00
|
|
|
type AuthState = {
|
|
|
|
|
loading: boolean
|
2026-09-19 16:45:24 +02:00
|
|
|
authenticated: boolean
|
|
|
|
|
canEdit: boolean
|
2026-09-19 17:33:44 +02:00
|
|
|
username?: string
|
2026-09-19 14:17:27 +02:00
|
|
|
error: string | null
|
2026-09-19 16:45:24 +02:00
|
|
|
refresh: () => Promise<void>
|
2026-09-19 16:51:24 +02:00
|
|
|
canSignIn: boolean
|
|
|
|
|
signIn: () => void
|
2026-09-19 17:33:44 +02:00
|
|
|
sudo: (username: string, password: string) => Promise<void>
|
|
|
|
|
signOut: () => void
|
2026-09-19 14:17:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const AuthContext = createContext<AuthState | undefined>(undefined)
|
|
|
|
|
|
|
|
|
|
export function AuthProvider({ children }: { children: ReactNode }) {
|
2026-09-19 16:45:24 +02:00
|
|
|
const [authenticated, setAuthenticated] = useState(false)
|
|
|
|
|
const [canEdit, setCanEdit] = useState(false)
|
2026-09-19 17:33:44 +02:00
|
|
|
const [username, setUsername] = useState<string | undefined>()
|
2026-09-19 14:17:27 +02:00
|
|
|
const [loading, setLoading] = useState(true)
|
|
|
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
|
|
|
|
|
|
const refresh = useCallback(async () => {
|
|
|
|
|
try {
|
|
|
|
|
setError(null)
|
|
|
|
|
const session = await requestSession()
|
2026-09-19 16:45:24 +02:00
|
|
|
setAuthenticated(session?.authenticated === true)
|
|
|
|
|
setCanEdit(session?.canEdit === true)
|
2026-09-19 17:33:44 +02:00
|
|
|
setUsername(session?.username)
|
2026-09-19 14:17:27 +02:00
|
|
|
} catch (err) {
|
2026-09-19 16:45:24 +02:00
|
|
|
setAuthenticated(false)
|
|
|
|
|
setCanEdit(false)
|
|
|
|
|
setError(err instanceof Error ? err.message : 'Unable to check your SSO session.')
|
2026-09-19 14:17:27 +02:00
|
|
|
} finally {
|
|
|
|
|
setLoading(false)
|
|
|
|
|
}
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
useEffect(() => { void refresh() }, [refresh])
|
|
|
|
|
|
2026-09-19 16:51:24 +02:00
|
|
|
const signIn = useCallback(() => { signInWithGoogle() }, [])
|
2026-09-19 17:33:44 +02:00
|
|
|
const sudo = useCallback(async (username: string, password: string) => {
|
|
|
|
|
const result = await sudoLogin(username, password)
|
|
|
|
|
window.sessionStorage.setItem('cms-demo-token', result.token)
|
|
|
|
|
setAuthenticated(true)
|
|
|
|
|
setCanEdit(true)
|
|
|
|
|
setUsername(result.user.username)
|
|
|
|
|
}, [])
|
|
|
|
|
const signOut = useCallback(() => {
|
|
|
|
|
window.sessionStorage.removeItem('cms-demo-token')
|
|
|
|
|
setAuthenticated(false)
|
|
|
|
|
setCanEdit(false)
|
|
|
|
|
setUsername(undefined)
|
|
|
|
|
}, [])
|
2026-09-19 16:51:24 +02:00
|
|
|
const canSignIn = apiConfig.signIn !== ''
|
|
|
|
|
|
|
|
|
|
const value = useMemo(
|
2026-09-19 17:33:44 +02:00
|
|
|
() => ({ loading, authenticated, canEdit, username, error, refresh, canSignIn, signIn, sudo, signOut }),
|
|
|
|
|
[loading, authenticated, canEdit, username, error, refresh, canSignIn, signIn, sudo, signOut],
|
2026-09-19 16:51:24 +02:00
|
|
|
)
|
2026-09-19 14:17:27 +02:00
|
|
|
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useAuth() {
|
|
|
|
|
const context = useContext(AuthContext)
|
|
|
|
|
if (!context) throw new Error('useAuth must be used inside AuthProvider')
|
|
|
|
|
return context
|
2026-09-19 16:45:24 +02:00
|
|
|
}
|