@pubflow/react 0.4.12 → 0.4.14

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.cjs CHANGED
@@ -254,6 +254,7 @@ function PubflowProvider({ children, config, instances, defaultInstance = 'defau
254
254
  });
255
255
  const apiClient = new core.ApiClient(fullConfig, storage);
256
256
  const authService = new core.AuthService(apiClient, storage, fullConfig);
257
+ const twoFactorService = new core.TwoFactorService(apiClient, `${fullConfig.authBasePath || '/auth'}/two_factor`);
257
258
  // Get current user (non-blocking)
258
259
  let user = null;
259
260
  let isAuthenticated = false;
@@ -261,9 +262,12 @@ function PubflowProvider({ children, config, instances, defaultInstance = 'defau
261
262
  config: fullConfig,
262
263
  apiClient,
263
264
  authService,
265
+ twoFactorService,
264
266
  user,
265
267
  isAuthenticated,
266
268
  isLoading: true,
269
+ twoFactorPending: false,
270
+ twoFactorMethods: [],
267
271
  login: async (credentials) => {
268
272
  const result = await authService.login(credentials);
269
273
  if (result.success && result.user && isMounted) {
@@ -280,6 +284,23 @@ function PubflowProvider({ children, config, instances, defaultInstance = 'defau
280
284
  }
281
285
  }));
282
286
  }
287
+ else if (result.requires2fa && isMounted) {
288
+ setContextValue(prev => {
289
+ var _a;
290
+ return ({
291
+ ...prev,
292
+ instances: {
293
+ ...prev.instances,
294
+ [instanceConfig.id]: {
295
+ ...prev.instances[instanceConfig.id],
296
+ twoFactorPending: true,
297
+ twoFactorMethods: (_a = result.availableMethods) !== null && _a !== void 0 ? _a : [],
298
+ isLoading: false
299
+ }
300
+ }
301
+ });
302
+ });
303
+ }
283
304
  return result;
284
305
  },
285
306
  logout: async () => {
@@ -293,7 +314,9 @@ function PubflowProvider({ children, config, instances, defaultInstance = 'defau
293
314
  ...prev.instances[instanceConfig.id],
294
315
  user: null,
295
316
  isAuthenticated: false,
296
- isLoading: false
317
+ isLoading: false,
318
+ twoFactorPending: false,
319
+ twoFactorMethods: []
297
320
  }
298
321
  }
299
322
  }));
@@ -316,7 +339,31 @@ function PubflowProvider({ children, config, instances, defaultInstance = 'defau
316
339
  }));
317
340
  }
318
341
  return result;
319
- }
342
+ },
343
+ startTwoFactor: async (methodId, method) => {
344
+ return twoFactorService.start(methodId, method, 'login');
345
+ },
346
+ verifyTwoFactor: async (methodId, code) => {
347
+ const result = await twoFactorService.verify(methodId, code, 'login');
348
+ if (result.verified && isMounted) {
349
+ const userData = await authService.getCurrentUser();
350
+ setContextValue(prev => ({
351
+ ...prev,
352
+ instances: {
353
+ ...prev.instances,
354
+ [instanceConfig.id]: {
355
+ ...prev.instances[instanceConfig.id],
356
+ user: userData,
357
+ isAuthenticated: true,
358
+ isLoading: false,
359
+ twoFactorPending: false,
360
+ twoFactorMethods: []
361
+ }
362
+ }
363
+ }));
364
+ }
365
+ return result;
366
+ },
320
367
  };
321
368
  // Load user asynchronously
322
369
  authService.getCurrentUser().then(currentUser => {
@@ -365,13 +412,17 @@ function PubflowProvider({ children, config, instances, defaultInstance = 'defau
365
412
  });
366
413
  const apiClient = new core.ApiClient(fullConfig, storage);
367
414
  const authService = new core.AuthService(apiClient, storage, fullConfig);
415
+ const twoFactorService = new core.TwoFactorService(apiClient, `${fullConfig.authBasePath || '/auth'}/two_factor`);
368
416
  instancesMap[defaultInstance] = {
369
417
  config: fullConfig,
370
418
  apiClient,
371
419
  authService,
420
+ twoFactorService,
372
421
  user: null,
373
422
  isAuthenticated: false,
374
423
  isLoading: true,
424
+ twoFactorPending: false,
425
+ twoFactorMethods: [],
375
426
  login: async (credentials) => {
376
427
  const result = await authService.login(credentials);
377
428
  if (result.success && result.user && isMounted) {
@@ -388,6 +439,23 @@ function PubflowProvider({ children, config, instances, defaultInstance = 'defau
388
439
  }
389
440
  }));
390
441
  }
442
+ else if (result.requires2fa && isMounted) {
443
+ setContextValue(prev => {
444
+ var _a;
445
+ return ({
446
+ ...prev,
447
+ instances: {
448
+ ...prev.instances,
449
+ [defaultInstance]: {
450
+ ...prev.instances[defaultInstance],
451
+ twoFactorPending: true,
452
+ twoFactorMethods: (_a = result.availableMethods) !== null && _a !== void 0 ? _a : [],
453
+ isLoading: false
454
+ }
455
+ }
456
+ });
457
+ });
458
+ }
391
459
  return result;
392
460
  },
393
461
  logout: async () => {
@@ -401,7 +469,9 @@ function PubflowProvider({ children, config, instances, defaultInstance = 'defau
401
469
  ...prev.instances[defaultInstance],
402
470
  user: null,
403
471
  isAuthenticated: false,
404
- isLoading: false
472
+ isLoading: false,
473
+ twoFactorPending: false,
474
+ twoFactorMethods: []
405
475
  }
406
476
  }
407
477
  }));
@@ -424,7 +494,31 @@ function PubflowProvider({ children, config, instances, defaultInstance = 'defau
424
494
  }));
425
495
  }
426
496
  return result;
427
- }
497
+ },
498
+ startTwoFactor: async (methodId, method) => {
499
+ return twoFactorService.start(methodId, method, 'login');
500
+ },
501
+ verifyTwoFactor: async (methodId, code) => {
502
+ const result = await twoFactorService.verify(methodId, code, 'login');
503
+ if (result.verified && isMounted) {
504
+ const userData = await authService.getCurrentUser();
505
+ setContextValue(prev => ({
506
+ ...prev,
507
+ instances: {
508
+ ...prev.instances,
509
+ [defaultInstance]: {
510
+ ...prev.instances[defaultInstance],
511
+ user: userData,
512
+ isAuthenticated: true,
513
+ isLoading: false,
514
+ twoFactorPending: false,
515
+ twoFactorMethods: []
516
+ }
517
+ }
518
+ }));
519
+ }
520
+ return result;
521
+ },
428
522
  };
429
523
  // Load user asynchronously
430
524
  authService.getCurrentUser().then(currentUser => {
@@ -10061,6 +10155,16 @@ function useTwoFactor(instanceId) {
10061
10155
  setIsLoading(false);
10062
10156
  }
10063
10157
  }, [instance, refresh]);
10158
+ const sendCode = React.useCallback(async (methodId, method, action = 'sensitive_action') => {
10159
+ if (!instance)
10160
+ return { success: false, error: 'Pubflow not initialized' };
10161
+ try {
10162
+ return await instance.twoFactorService.start(methodId, method, action);
10163
+ }
10164
+ catch (e) {
10165
+ return { success: false, error: (e === null || e === void 0 ? void 0 : e.message) || 'Failed to send code' };
10166
+ }
10167
+ }, [instance]);
10064
10168
  return {
10065
10169
  systemEnabled,
10066
10170
  availableMethods,
@@ -10072,6 +10176,7 @@ function useTwoFactor(instanceId) {
10072
10176
  confirmSetup,
10073
10177
  toggle,
10074
10178
  removeMethod,
10179
+ sendCode,
10075
10180
  };
10076
10181
  }
10077
10182