Insolitum Developers
API Reference

Hooks Reference

API reference for useShellAuth, useModuleApi, and useNats hooks.

Hooks Reference

useShellAuth

Authentication hook — provides user info, Supabase client, and organization context.

Import: import { useShellAuth } from '@/lib/shell-auth';

Returns

interface ShellAuthContextType {
  user: User | null;            // Supabase User object
  supabase: SupabaseClient;     // Authenticated Supabase client
  organizationId: string | null; // Current tenant UUID (from URL params)
  isAuthenticated: boolean;      // true when user is set
  isLoading: boolean;           // true during auth handshake (max 5s)
}

Example

'use client';
import { useShellAuth } from '@/lib/shell-auth';
 
export default function MyPage() {
  const { user, organizationId, isAuthenticated, isLoading } = useShellAuth();
 
  if (isLoading) return <LoadingSpinner />;
  if (!isAuthenticated) return <p>Not authenticated</p>;
 
  return <p>Logged in as {user?.email} for org {organizationId}</p>;
}

useModuleApi

Tenant-scoped database operations hook.

Import: import { useModuleApi } from '@/hooks/useModuleApi';

Returns

{
  supabase: SupabaseClient;          // Raw Supabase client
  organizationId: string | null;     // Current tenant
  query: (table: string) => PostgrestFilterBuilder;  // SELECT with auto org filter
  insert: (table: string, data: Record<string, unknown>) => Promise;  // INSERT with auto org_id
  update: (table: string, data: Record, filters: Record) => Promise;  // UPDATE scoped to tenant
  remove: (table: string, filters: Record) => Promise;  // DELETE scoped to tenant
}

Example

const { query, insert, update, remove } = useModuleApi();
 
// Select all items for current org
const { data } = await query('mymodule_items').select('*');
 
// Insert with auto organization_id
await insert('mymodule_items', { name: 'New Item' });
 
// Update where id matches, scoped to org
await update('mymodule_items', { name: 'Updated' }, { id: 'abc' });
 
// Delete where id matches, scoped to org
await remove('mymodule_items', { id: 'abc' });

useNats

Real-time event subscription hook via WebSocket gateway.

Import: import { useNats } from '@/hooks/useNats';

Returns

{
  connected: boolean;
  subscribe: (channel: WsChannel, callback: (event: NatsEvent) => void) => () => void;
  publish: (subject: string, data: unknown) => void;
}

WsChannel Type

type WsChannel = 'telemetry' | 'alerts' | 'events' | 'modules' | 'system';

Example

const { connected, subscribe, publish } = useNats();
 
// Subscribe
useEffect(() => {
  if (!connected) return;
  const unsub = subscribe('telemetry', (event) => {
    console.log(event.subject, event.data);
  });
  return unsub;
}, [connected, subscribe]);
 
// Publish
publish('modules.my-module.custom-event', {
  type: 'notification',
  message: 'Hello from my module!',
});

On this page