@pubflow/react 0.4.6 → 0.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import * as _pubflow_core from '@pubflow/core';
2
- import { EntityData, BridgeApiService, EntityConfig, FilterOperator, FilterDefinition, PubflowInstanceConfig, ApiClient, AuthService, User, QueryParams, SearchParams, StorageAdapter } from '@pubflow/core';
2
+ import { EntityData, BridgeApiService, EntityConfig, FilterOperator, FilterDefinition, PubflowInstanceConfig, ApiClient, AuthService, TwoFactorService, User, TwoFactorMethod, TwoFactorVerifyResult, TwoFactorStartResult, QueryParams, SearchParams, TwoFactorSetupResult, TwoFactorToggleResult, StorageAdapter } from '@pubflow/core';
3
3
  export * from '@pubflow/core';
4
+ export { AddOrganizationMemberRequest, Address, AddressType, BridgePaymentClient, BridgePaymentConfig, CancelSubscriptionRequest, ConvertGuestToUserRequest, ConvertGuestToUserResponse, CreateAddressRequest, CreateCustomerRequest, CreateOrganizationRequest, CreatePaymentIntentRequest, CreateSubscriptionRequest, Customer, ListResponse, Organization, OrganizationMember, OrganizationRole, PaginationParams, Payment, PaymentIntent, PaymentMethod, PaymentMethodType, PaymentRequestOptions, Subscription, SubscriptionStatus, UpdateAddressRequest, UpdateCustomerRequest, UpdateOrganizationMemberRoleRequest, UpdateOrganizationRequest, UpdatePaymentMethodRequest } from '@pubflow/core';
4
5
  import * as react_jsx_runtime from 'react/jsx-runtime';
5
6
  import React, { CSSProperties, ReactNode } from 'react';
6
7
  import { z } from 'zod';
@@ -403,7 +404,7 @@ interface BridgeTableProps<T extends EntityData> extends UseBridgeCrudOptions<T>
403
404
  /**
404
405
  * Component for displaying data in a table with sorting, filtering, and pagination
405
406
  */
406
- declare function BridgeTable<T extends EntityData>({ columns, showPagination, showSearch, showFilters, rowKey, onRowClick, className, loadingComponent, emptyComponent, errorComponent, paginationComponent, searchComponent, filterComponent, searchPlaceholder, actions, ...crudOptions }: BridgeTableProps<T>): string | number | true | Iterable<React.ReactNode> | react_jsx_runtime.JSX.Element;
407
+ declare function BridgeTable<T extends EntityData>({ columns, showPagination, showSearch, showFilters, rowKey, onRowClick, className, loadingComponent, emptyComponent, errorComponent, paginationComponent, searchComponent, filterComponent, searchPlaceholder, actions, ...crudOptions }: BridgeTableProps<T>): string | number | bigint | true | Iterable<React.ReactNode> | Promise<string | number | bigint | boolean | React.ReactPortal | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | Iterable<React.ReactNode> | null | undefined> | react_jsx_runtime.JSX.Element;
407
408
 
408
409
  /**
409
410
  * Types for BridgeForm component
@@ -1284,6 +1285,7 @@ declare function createStyles(theme: ThemeConfig): {
1284
1285
  interface PubflowContextValue {
1285
1286
  instances: Record<string, PubflowInstance>;
1286
1287
  defaultInstance: string;
1288
+ isReady: boolean;
1287
1289
  }
1288
1290
  /**
1289
1291
  * Pubflow instance
@@ -1292,9 +1294,12 @@ interface PubflowInstance {
1292
1294
  config: PubflowInstanceConfig;
1293
1295
  apiClient: ApiClient;
1294
1296
  authService: AuthService;
1297
+ twoFactorService: TwoFactorService;
1295
1298
  user: User | null | undefined;
1296
1299
  isAuthenticated: boolean;
1297
1300
  isLoading: boolean;
1301
+ twoFactorPending: boolean;
1302
+ twoFactorMethods: TwoFactorMethod[];
1298
1303
  login: (credentials: {
1299
1304
  email?: string;
1300
1305
  userName?: string;
@@ -1306,28 +1311,13 @@ interface PubflowInstance {
1306
1311
  expiresAt?: string;
1307
1312
  user?: User;
1308
1313
  }>;
1314
+ verifyTwoFactor: (methodId: string, code: string) => Promise<TwoFactorVerifyResult>;
1315
+ startTwoFactor: (methodId: string, method: string) => Promise<TwoFactorStartResult>;
1309
1316
  }
1310
1317
  /**
1311
1318
  * Create Pubflow context
1312
1319
  */
1313
1320
  declare const PubflowContext: React.Context<PubflowContextValue>;
1314
- /**
1315
- * Persistent cache configuration
1316
- */
1317
- interface PersistentCacheConfig {
1318
- /**
1319
- * Whether to enable persistent cache
1320
- */
1321
- enabled: boolean;
1322
- /**
1323
- * Cache provider function
1324
- */
1325
- provider?: () => Map<any, any>;
1326
- /**
1327
- * Cache options
1328
- */
1329
- options?: any;
1330
- }
1331
1321
  /**
1332
1322
  * Pubflow provider props
1333
1323
  */
@@ -1336,41 +1326,25 @@ interface PubflowProviderProps {
1336
1326
  config?: PubflowInstanceConfig;
1337
1327
  instances?: PubflowInstanceConfig[];
1338
1328
  defaultInstance?: string;
1339
- onSessionExpired?: () => void;
1340
- onSessionRefreshed?: () => void;
1341
- showSessionAlerts?: boolean;
1342
- /**
1343
- * Persistent cache configuration
1344
- */
1345
- persistentCache?: PersistentCacheConfig;
1346
- /**
1347
- * Login redirect path (for automatic redirects)
1348
- */
1349
- loginRedirectPath?: string;
1350
- /**
1351
- * Public paths that don't require authentication
1352
- */
1353
- publicPaths?: string[];
1354
- /**
1355
- * Enable debug tools
1356
- * When enabled, debug utilities will be available
1357
- * Default: false
1358
- */
1359
1329
  enableDebugTools?: boolean;
1360
- /**
1361
- * Theme configuration
1362
- */
1330
+ showSessionAlerts?: boolean;
1331
+ persistentCache?: {
1332
+ enabled: boolean;
1333
+ maxAge?: number;
1334
+ };
1363
1335
  theme?: {
1364
1336
  primaryColor?: string;
1365
1337
  secondaryColor?: string;
1366
1338
  appName?: string;
1367
- logo?: string | React.ReactNode;
1339
+ logo?: string;
1368
1340
  };
1341
+ loginRedirectPath?: string;
1342
+ publicPaths?: string[];
1369
1343
  }
1370
1344
  /**
1371
- * Pubflow provider for React
1345
+ * Pubflow Provider Component
1372
1346
  */
1373
- declare function PubflowProvider({ children, config, instances, defaultInstance, onSessionExpired, onSessionRefreshed, showSessionAlerts, persistentCache, loginRedirectPath, publicPaths, enableDebugTools, theme }: PubflowProviderProps): react_jsx_runtime.JSX.Element | null;
1347
+ declare function PubflowProvider({ children, config, instances, defaultInstance, enableDebugTools, showSessionAlerts, persistentCache, theme, loginRedirectPath, publicPaths }: PubflowProviderProps): react_jsx_runtime.JSX.Element;
1374
1348
 
1375
1349
  /**
1376
1350
  * Authentication Hook for React
@@ -1385,6 +1359,10 @@ interface UseAuthResult {
1385
1359
  user: User | null;
1386
1360
  isAuthenticated: boolean;
1387
1361
  isLoading: boolean;
1362
+ /** Whether the user is in the 2FA pending step (session is partial) */
1363
+ twoFactorPending: boolean;
1364
+ /** Available 2FA methods for the pending step */
1365
+ twoFactorMethods: TwoFactorMethod[];
1388
1366
  login: (credentials: {
1389
1367
  email?: string;
1390
1368
  userName?: string;
@@ -1395,7 +1373,10 @@ interface UseAuthResult {
1395
1373
  isValid: boolean;
1396
1374
  expiresAt?: string;
1397
1375
  }>;
1398
- refreshUser: () => Promise<User | null>;
1376
+ /** Submit a 2FA verification code to complete a pending login */
1377
+ verifyTwoFactor: (methodId: string, code: string) => Promise<TwoFactorVerifyResult>;
1378
+ /** (Re-)send a 2FA code for the given method */
1379
+ startTwoFactor: (methodId: string, method: string) => Promise<TwoFactorStartResult>;
1399
1380
  }
1400
1381
  /**
1401
1382
  * Hook for accessing authentication functionality
@@ -1405,6 +1386,84 @@ interface UseAuthResult {
1405
1386
  */
1406
1387
  declare function useAuth(instanceId?: string): UseAuthResult;
1407
1388
 
1389
+ /**
1390
+ * Authentication Guard Hook for React
1391
+ *
1392
+ * Framework-agnostic hook for handling authentication validation and redirects
1393
+ */
1394
+ /**
1395
+ * Authentication guard options
1396
+ */
1397
+ interface UseAuthGuardOptions {
1398
+ /**
1399
+ * Whether to validate the session on mount
1400
+ */
1401
+ validateOnMount?: boolean;
1402
+ /**
1403
+ * User types allowed to access the page
1404
+ */
1405
+ allowedTypes?: string[];
1406
+ /**
1407
+ * Pubflow instance ID
1408
+ */
1409
+ instanceId?: string;
1410
+ /**
1411
+ * Custom redirect function (framework-specific)
1412
+ */
1413
+ onRedirect?: (path: string, reason: 'unauthenticated' | 'unauthorized' | 'session-expired') => void;
1414
+ /**
1415
+ * Custom session expired handler
1416
+ */
1417
+ onSessionExpired?: () => void;
1418
+ /**
1419
+ * Login redirect path
1420
+ */
1421
+ loginRedirectPath?: string;
1422
+ /**
1423
+ * Access denied redirect path
1424
+ */
1425
+ accessDeniedPath?: string;
1426
+ /**
1427
+ * Whether to show console warnings
1428
+ */
1429
+ enableLogging?: boolean;
1430
+ }
1431
+ /**
1432
+ * Authentication guard result
1433
+ */
1434
+ interface UseAuthGuardResult {
1435
+ user: any;
1436
+ isAuthenticated: boolean;
1437
+ isLoading: boolean;
1438
+ isAuthorized: boolean;
1439
+ validateSession: () => Promise<{
1440
+ isValid: boolean;
1441
+ expiresAt?: string;
1442
+ user?: any;
1443
+ }>;
1444
+ redirectReason: 'unauthenticated' | 'unauthorized' | 'session-expired' | null;
1445
+ }
1446
+ /**
1447
+ * Hook for handling authentication validation and redirects
1448
+ *
1449
+ * This hook is framework-agnostic and requires the user to provide
1450
+ * their own redirect function (e.g., Next.js router, React Router, etc.)
1451
+ *
1452
+ * @param options Hook options
1453
+ * @returns Authentication guard result
1454
+ */
1455
+ declare function useAuthGuard({ validateOnMount, allowedTypes, instanceId, onRedirect, onSessionExpired, loginRedirectPath, accessDeniedPath, enableLogging }?: UseAuthGuardOptions): UseAuthGuardResult;
1456
+ /**
1457
+ * Simple authentication guard hook with automatic redirects
1458
+ *
1459
+ * This is a simplified version that uses window.location for redirects
1460
+ * Suitable for simple React apps without complex routing
1461
+ *
1462
+ * @param options Hook options
1463
+ * @returns Authentication guard result
1464
+ */
1465
+ declare function useSimpleAuthGuard(options?: Omit<UseAuthGuardOptions, 'onRedirect'>): UseAuthGuardResult;
1466
+
1408
1467
  /**
1409
1468
  * Bridge API Hook for React
1410
1469
  *
@@ -1556,50 +1615,55 @@ interface UseBridgeQueryResult<T extends EntityData, Q extends QueryType> {
1556
1615
  declare function useBridgeQuery<T extends EntityData, Q extends QueryType>(service: BridgeApiService<T>, type: Q, params: BridgeQueryParams<Q>, config?: SWRConfiguration): UseBridgeQueryResult<T, Q>;
1557
1616
 
1558
1617
  /**
1559
- * Server Authentication Hook for React
1618
+ * useTwoFactor Hook for React
1560
1619
  *
1561
- * Provides a hook for handling authentication with automatic redirects
1562
- */
1563
- /**
1564
- * Server authentication hook options
1620
+ * Convenience hook for managing 2FA settings (setup, toggle, remove).
1621
+ * For the LOGIN-TIME 2FA flow use useAuth() which surfaces
1622
+ * twoFactorPending / verifyTwoFactor / startTwoFactor.
1565
1623
  */
1566
- interface UseServerAuthOptions {
1624
+
1625
+ interface UseTwoFactorResult {
1626
+ /** Whether the 2FA system is globally enabled on this server */
1627
+ systemEnabled: boolean;
1628
+ /** Available methods on the server (e.g. ['email']) */
1629
+ availableMethods: string[];
1630
+ /** User's configured 2FA methods (status = active only) */
1631
+ methods: TwoFactorMethod[];
1632
+ isLoading: boolean;
1633
+ error: string | null;
1634
+ /** Load system info and user methods */
1635
+ refresh: () => Promise<void>;
1567
1636
  /**
1568
- * Path to redirect to when not authenticated
1637
+ * Step 1 of setup: register method + send verification code.
1638
+ * Returns { success, method_id, status: 'pending_setup', ... }
1569
1639
  */
1570
- loginRedirectPath?: string;
1640
+ setup: (method: string, identifier: string) => Promise<TwoFactorSetupResult>;
1571
1641
  /**
1572
- * User types allowed to access the page
1642
+ * Step 2 of setup: confirm the code received → activates the method.
1573
1643
  */
1574
- allowedTypes?: string[];
1644
+ confirmSetup: (methodId: string, method: string, code: string) => Promise<TwoFactorVerifyResult>;
1575
1645
  /**
1576
- * Whether to validate the session on mount
1577
- */
1578
- validateOnMount?: boolean;
1579
- /**
1580
- * Pubflow instance ID
1646
+ * Enable or disable 2FA for the user.
1647
+ * When disabling with existing methods a verification code is required.
1581
1648
  */
1582
- instanceId?: string;
1649
+ toggle: (enabled: boolean, verificationCode?: string, verificationMethodId?: string) => Promise<TwoFactorToggleResult>;
1583
1650
  /**
1584
- * Custom redirect function
1651
+ * Remove a 2FA method. Requires a verification code from another active method.
1585
1652
  */
1586
- onRedirect?: (path: string) => void;
1653
+ removeMethod: (methodId: string, verificationCode: string, verificationMethodId: string) => Promise<{
1654
+ success: boolean;
1655
+ message?: string;
1656
+ error?: string;
1657
+ }>;
1587
1658
  }
1588
1659
  /**
1589
- * Hook for handling authentication with automatic redirects
1660
+ * Hook for managing 2FA settings.
1590
1661
  *
1591
- * @param options Hook options
1592
- * @returns Authentication hook result
1662
+ * Must be used inside a PubflowProvider.
1663
+ *
1664
+ * @param instanceId Optional Pubflow instance ID
1593
1665
  */
1594
- declare function useServerAuth({ loginRedirectPath, allowedTypes, validateOnMount, instanceId, onRedirect }?: UseServerAuthOptions): {
1595
- user: _pubflow_core.User | null;
1596
- isAuthenticated: boolean;
1597
- isLoading: boolean;
1598
- validateSession: () => Promise<{
1599
- isValid: boolean;
1600
- expiresAt?: string;
1601
- }>;
1602
- };
1666
+ declare function useTwoFactor(instanceId?: string): UseTwoFactorResult;
1603
1667
 
1604
1668
  /**
1605
1669
  * Search Query Builder Hook for React
@@ -1905,4 +1969,4 @@ declare function useRequireAuth(options?: UseRequireAuthOptions): {
1905
1969
  isLoading: boolean;
1906
1970
  };
1907
1971
 
1908
- export { AccountCreationForm, AccountCreationFormConfig, AccountCreationFormProps, AdvancedFilter, AdvancedFilterColors, AdvancedFilterProps, AdvancedFilterTexts, BridgeForm, BridgeFormProps, BridgeList, BridgeListColors, BridgeListLayout, BridgeListProps, BridgeListRenderItemProps, BridgeListTexts, BridgeTable, BridgeTableProps, BridgeView, BridgeViewProps, BrowserStorageAdapter, BrowserStorageOptions, ColumnDef, FieldConfig, FieldRenderProps, FilterField, LoginForm, LoginFormConfig, LoginFormProps, MutationOptions, OfflineIndicator, OfflineIndicatorProps, PasswordResetForm, PasswordResetFormConfig, PasswordResetFormProps, PersistentCacheConfig, PubflowContext, PubflowContextValue, PubflowInstance, PubflowProvider, PubflowProviderProps, RawApiRequestOptions, RawApiResponse, SearchField, SearchQueryState, ThemeConfig, ThemeProvider, ThemeProviderProps, UseAuthResult, UseBridgeApiRawResult, UseBridgeMutationResult, UseBridgeQueryResult, UseRequireAuthOptions, UseSearchQueryBuilderResult, UseServerAuthOptions, createStyles, generateCSSVariables, useAuth, useBridgeApi, useBridgeApiRaw, useBridgeCrud, useBridgeMutation, useBridgeQuery, useNetworkStatus, useRequireAuth, useSearchQueryBuilder, useServerAuth, useTheme };
1972
+ export { AccountCreationForm, AccountCreationFormConfig, AccountCreationFormProps, AdvancedFilter, AdvancedFilterColors, AdvancedFilterProps, AdvancedFilterTexts, BridgeForm, BridgeFormProps, BridgeList, BridgeListColors, BridgeListLayout, BridgeListProps, BridgeListRenderItemProps, BridgeListTexts, BridgeTable, BridgeTableProps, BridgeView, BridgeViewProps, BrowserStorageAdapter, BrowserStorageOptions, ColumnDef, FieldConfig, FieldRenderProps, FilterField, LoginForm, LoginFormConfig, LoginFormProps, MutationOptions, OfflineIndicator, OfflineIndicatorProps, PasswordResetForm, PasswordResetFormConfig, PasswordResetFormProps, PubflowContext, PubflowContextValue, PubflowInstance, PubflowProvider, PubflowProviderProps, RawApiRequestOptions, RawApiResponse, SearchField, SearchQueryState, ThemeConfig, ThemeProvider, ThemeProviderProps, UseAuthGuardOptions, UseAuthGuardResult, UseAuthResult, UseBridgeApiRawResult, UseBridgeMutationResult, UseBridgeQueryResult, UseRequireAuthOptions, UseSearchQueryBuilderResult, UseTwoFactorResult, createStyles, generateCSSVariables, useAuth, useAuthGuard, useBridgeApi, useBridgeApiRaw, useBridgeCrud, useBridgeMutation, useBridgeQuery, useNetworkStatus, useRequireAuth, useSearchQueryBuilder, useSimpleAuthGuard, useTheme, useTwoFactor };
package/dist/index.esm.js CHANGED
@@ -10142,6 +10142,27 @@ function useTwoFactor(instanceId) {
10142
10142
  setIsLoading(false);
10143
10143
  }
10144
10144
  }, [instance, refresh]);
10145
+ const confirmSetup = useCallback(async (methodId, method, code) => {
10146
+ if (!instance)
10147
+ return { success: false, verified: false, error: 'Pubflow not initialized' };
10148
+ setIsLoading(true);
10149
+ setError(null);
10150
+ try {
10151
+ // action='setup' activates a pending_setup method in the DB
10152
+ const result = await instance.twoFactorService.verify(methodId, code, 'setup');
10153
+ if (result.verified)
10154
+ await refresh();
10155
+ return result;
10156
+ }
10157
+ catch (e) {
10158
+ const msg = (e === null || e === void 0 ? void 0 : e.message) || 'Confirmation failed';
10159
+ setError(msg);
10160
+ return { success: false, verified: false, error: msg };
10161
+ }
10162
+ finally {
10163
+ setIsLoading(false);
10164
+ }
10165
+ }, [instance, refresh]);
10145
10166
  return {
10146
10167
  systemEnabled,
10147
10168
  availableMethods,
@@ -10150,6 +10171,7 @@ function useTwoFactor(instanceId) {
10150
10171
  error,
10151
10172
  refresh,
10152
10173
  setup,
10174
+ confirmSetup,
10153
10175
  toggle,
10154
10176
  removeMethod,
10155
10177
  };