Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript
5.7 KB
Raw Blame
'use client'

import React, {
  createContext,
  useCallback,
  useContext,
  useEffect,
  useMemo,
  useRef,
  useState,
} from 'react'
import { useRouter } from 'next/navigation'
import { CONTROL_UI_SESSION_INVALIDATION_EVENT } from '@constants/readRequests'
import { CONTROL_ROUTES } from '@constants/routes'
import {
  type AdminLoginResponse,
  clearAdminAuthToken,
  getControlUIAuthMe,
  isSilentApiError,
  loginControlUI,
  logoutControlUI,
  setControlUIReadPrincipal,
  setGlobalAuthErrorHandler,
} from '../lib/api'
import { buildControlUiLoginPath, getCurrentControlUiPath } from '../lib/authRedirect'
import { resetPublishScopeCache } from '../lib/hooks/usePublishScope'
import { invalidateRegistryCapabilityCache } from '../lib/hooks/useRegistryCapability'
import { useToast } from './Toast'

type AuthState = {
  id: string
  isLoggedIn: boolean
  isLoading: boolean
  username: string
  email: string
}

type AuthContextValue = {
  authState: AuthState
  login: (username: string, password: string) => Promise<AdminLoginResponse>
  logout: () => Promise<void>
  checkAuth: () => Promise<void>
}

const AuthContext = createContext<AuthContextValue | undefined>(undefined)

export function AuthProvider({ children }: { children: React.ReactNode }) {
  const router = useRouter()
  const { showToast } = useToast()
  const [authState, setAuthState] = useState<AuthState>({
    id: '',
    isLoggedIn: false,
    isLoading: true,
    username: '',
    email: '',
  })
  const sessionExpiredToastShownRef = useRef(false)
  const authOperationRef = useRef(0)

  // The router and ToastProvider callbacks are stable in normal runtime; keep
  // this singleton registration tied to them if either provider is remounted.
  useEffect(() => {
    const handleAuthError = () => {
      authOperationRef.current += 1
      if (sessionExpiredToastShownRef.current) return
      sessionExpiredToastShownRef.current = true
      resetPublishScopeCache()
      invalidateRegistryCapabilityCache()
      setAuthState({ id: '', isLoggedIn: false, isLoading: false, username: '', email: '' })
      router.replace(buildControlUiLoginPath(getCurrentControlUiPath()))
      showToast('Session expired. Please sign in again.', { tone: 'error' })
    }
    setGlobalAuthErrorHandler(handleAuthError)
  }, [router, showToast])

  const checkAuth = useCallback(async () => {
    const operation = ++authOperationRef.current
    try {
      const response = await getControlUIAuthMe()
      if (operation !== authOperationRef.current) return
      const { id, username, email, role } = response.me
      if (!id) {
        clearAdminAuthToken()
        setAuthState({ id: '', isLoggedIn: false, isLoading: false, username: '', email: '' })
        return
      }
      setControlUIReadPrincipal(id, role || 'admin')
      sessionExpiredToastShownRef.current = false
      setAuthState(prev => {
        if (operation !== authOperationRef.current) return prev
        const sameIdentity = prev.id === id
        return {
          id,
          isLoggedIn: true,
          isLoading: false,
          username: username ?? (sameIdentity ? prev.username : ''),
          email: email === undefined ? (sameIdentity ? prev.email : '') : (email ?? ''),
        }
      })
    } catch (error) {
      if (operation !== authOperationRef.current) return
      // Session-expiry errors are handled by the global toast/redirect path.
      if (isSilentApiError(error)) {
        setAuthState({ id: '', isLoggedIn: false, isLoading: false, username: '', email: '' })
        return
      }
      clearAdminAuthToken()
      setAuthState({ id: '', isLoggedIn: false, isLoading: false, username: '', email: '' })
    }
  }, [])

  useEffect(() => {
    void checkAuth()
    const onRemoteInvalidation = () => void checkAuth()
    window.addEventListener(CONTROL_UI_SESSION_INVALIDATION_EVENT, onRemoteInvalidation)
    return () => {
      authOperationRef.current += 1
      window.removeEventListener(CONTROL_UI_SESSION_INVALIDATION_EVENT, onRemoteInvalidation)
    }
  }, [checkAuth])

  const login = useCallback(
    async (username: string, password: string): Promise<AdminLoginResponse> => {
      const operation = ++authOperationRef.current
      const result = await loginControlUI(username, password)
      if (operation !== authOperationRef.current) return result
      setControlUIReadPrincipal(result.me.id, 'admin')
      resetPublishScopeCache()
      invalidateRegistryCapabilityCache()
      sessionExpiredToastShownRef.current = false
      setAuthState({
        id: result.me.id,
        isLoggedIn: true,
        isLoading: false,
        username: result.me.username || username,
        email: result.me.email || '',
      })
      return result
    },
    []
  )

  const logout = useCallback(async () => {
    const operation = ++authOperationRef.current
    try {
      await logoutControlUI()
    } finally {
      if (operation !== authOperationRef.current) return
      resetPublishScopeCache(authState.id)
      sessionExpiredToastShownRef.current = false
      // Drop the module-level registry-capability cache so a same-tab
      // logout→login doesn't serve the previous user's org identity.
      invalidateRegistryCapabilityCache()
      setAuthState({ id: '', isLoggedIn: false, isLoading: false, username: '', email: '' })
      router.replace(CONTROL_ROUTES.login)
    }
  }, [authState.id, router])

  const value = useMemo(
    () => ({ authState, login, logout, checkAuth }),
    [authState, checkAuth, login, logout]
  )

  return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>
}

export function useAuth() {
  const context = useContext(AuthContext)
  if (!context) {
    throw new Error('useAuth must be used within an AuthProvider')
  }
  return context
}