@turnkey/sdk-react 5.1.0 → 5.2.0

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.
@@ -4,6 +4,7 @@ import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
4
4
  import styles from './Auth.module.css.mjs';
5
5
  import React, { useState, useEffect } from 'react';
6
6
  import { MuiPhone } from './PhoneInput.mjs';
7
+ import { jwtDecode } from 'jwt-decode';
7
8
  import GoogleAuthButton from './Google.mjs';
8
9
  import AppleAuthButton from './Apple.mjs';
9
10
  import FacebookAuthButton from './Facebook.mjs';
@@ -398,107 +399,307 @@ var Auth = function Auth(_ref) {
398
399
  };
399
400
  }();
400
401
  var handleOAuthLogin = /*#__PURE__*/function () {
401
- var _ref8 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(credential, providerName) {
402
- var pubKey, createSuborgData, resp, suborgIds, suborgId, sessionResponse;
403
- return _regeneratorRuntime().wrap(function _callee6$(_context6) {
404
- while (1) switch (_context6.prev = _context6.next) {
402
+ var _ref8 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(credential, providerName) {
403
+ var _yield$server$getSubo;
404
+ var socialLinking,
405
+ pubKey,
406
+ oauthProviders,
407
+ createSuborgData,
408
+ completeLogin,
409
+ resp,
410
+ suborgIds,
411
+ oidcOrgIds,
412
+ orgId,
413
+ _ref10,
414
+ oidcEmail,
415
+ iss,
416
+ _yield$server$getVeri,
417
+ emailOrgIds,
418
+ _yield$server$getUser,
419
+ _yield$server$getUser2,
420
+ _yield$server$getUser3,
421
+ userId,
422
+ _yield$server$createS,
423
+ _yield$server$createS2,
424
+ _args7 = arguments;
425
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
426
+ while (1) switch (_context7.prev = _context7.next) {
405
427
  case 0:
406
- _context6.next = 2;
428
+ socialLinking = _args7.length > 2 && _args7[2] !== undefined ? _args7[2] : false;
429
+ _context7.next = 3;
407
430
  return indexedDbClient === null || indexedDbClient === void 0 ? void 0 : indexedDbClient.getPublicKey();
408
- case 2:
409
- pubKey = _context6.sent;
431
+ case 3:
432
+ pubKey = _context7.sent;
410
433
  if (pubKey) {
411
- _context6.next = 5;
434
+ _context7.next = 6;
412
435
  break;
413
436
  }
414
- return _context6.abrupt("return");
415
- case 5:
437
+ return _context7.abrupt("return");
438
+ case 6:
416
439
  setOauthLoading(providerName);
417
- createSuborgData = {
418
- oauthProviders: [{
419
- providerName: providerName,
420
- oidcToken: credential
421
- }]
422
- };
423
- if (customAccounts) {
424
- createSuborgData.customAccounts = customAccounts;
440
+ oauthProviders = [{
441
+ providerName: providerName,
442
+ oidcToken: credential
443
+ }];
444
+ createSuborgData = _objectSpread2({
445
+ oauthProviders: oauthProviders
446
+ }, customAccounts && {
447
+ customAccounts: customAccounts
448
+ });
449
+ completeLogin = /*#__PURE__*/function () {
450
+ var _ref9 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(suborgID) {
451
+ var sessionResponse;
452
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
453
+ while (1) switch (_context6.prev = _context6.next) {
454
+ case 0:
455
+ _context6.next = 2;
456
+ return server.oauthLogin({
457
+ suborgID: suborgID,
458
+ oidcToken: credential,
459
+ publicKey: pubKey,
460
+ sessionLengthSeconds: authConfig.sessionLengthSeconds
461
+ });
462
+ case 2:
463
+ sessionResponse = _context6.sent;
464
+ if (!(sessionResponse !== null && sessionResponse !== void 0 && sessionResponse.session)) {
465
+ _context6.next = 7;
466
+ break;
467
+ }
468
+ _context6.next = 6;
469
+ return indexedDbClient.loginWithSession(sessionResponse.session);
470
+ case 6:
471
+ return _context6.abrupt("return", onAuthSuccess());
472
+ case 7:
473
+ onError(authErrors.oauth.loginFailed);
474
+ case 8:
475
+ case "end":
476
+ return _context6.stop();
477
+ }
478
+ }, _callee6);
479
+ }));
480
+ return function completeLogin(_x6) {
481
+ return _ref9.apply(this, arguments);
482
+ };
483
+ }(); // Non-social-linking path: getOrCreateSuborg then login
484
+ if (socialLinking) {
485
+ _context7.next = 18;
486
+ break;
425
487
  }
426
- _context6.next = 10;
488
+ _context7.next = 13;
427
489
  return server.getOrCreateSuborg({
428
490
  filterType: FilterType.OidcToken,
429
491
  filterValue: credential,
430
492
  additionalData: createSuborgData
431
493
  });
432
- case 10:
433
- resp = _context6.sent;
494
+ case 13:
495
+ resp = _context7.sent;
434
496
  suborgIds = resp === null || resp === void 0 ? void 0 : resp.subOrganizationIds;
435
- if (!(!suborgIds || suborgIds.length === 0)) {
436
- _context6.next = 15;
497
+ if (suborgIds !== null && suborgIds !== void 0 && suborgIds.length) {
498
+ _context7.next = 17;
437
499
  break;
438
500
  }
439
- onError(authErrors.oauth.loginFailed);
440
- return _context6.abrupt("return");
441
- case 15:
442
- suborgId = suborgIds[0];
443
- _context6.next = 18;
444
- return server.oauthLogin({
445
- suborgID: suborgId,
446
- oidcToken: credential,
447
- publicKey: pubKey,
448
- sessionLengthSeconds: authConfig.sessionLengthSeconds
449
- });
501
+ return _context7.abrupt("return", onError(authErrors.oauth.loginFailed));
502
+ case 17:
503
+ return _context7.abrupt("return", completeLogin(suborgIds[0]));
450
504
  case 18:
451
- sessionResponse = _context6.sent;
452
- if (!(sessionResponse && sessionResponse.session)) {
453
- _context6.next = 26;
505
+ _context7.next = 20;
506
+ return server.getSuborgs({
507
+ filterType: FilterType.OidcToken,
508
+ filterValue: credential
509
+ });
510
+ case 20:
511
+ _context7.t1 = _yield$server$getSubo = _context7.sent;
512
+ _context7.t0 = _context7.t1 === null;
513
+ if (_context7.t0) {
514
+ _context7.next = 24;
454
515
  break;
455
516
  }
456
- _context6.next = 22;
457
- return indexedDbClient.loginWithSession(sessionResponse.session);
458
- case 22:
459
- _context6.next = 24;
460
- return onAuthSuccess();
517
+ _context7.t0 = _yield$server$getSubo === void 0;
461
518
  case 24:
462
- _context6.next = 27;
519
+ if (!_context7.t0) {
520
+ _context7.next = 28;
521
+ break;
522
+ }
523
+ _context7.t2 = void 0;
524
+ _context7.next = 29;
463
525
  break;
464
- case 26:
465
- onError(authErrors.oauth.loginFailed);
466
- case 27:
526
+ case 28:
527
+ _context7.t2 = _yield$server$getSubo.organizationIds;
528
+ case 29:
529
+ oidcOrgIds = _context7.t2;
530
+ orgId = oidcOrgIds === null || oidcOrgIds === void 0 ? void 0 : oidcOrgIds[0]; // If not, try to link by verified email (Google only)
531
+ if (orgId) {
532
+ _context7.next = 93;
533
+ break;
534
+ }
535
+ _ref10 = jwtDecode(credential) || {}, oidcEmail = _ref10.email, iss = _ref10.iss;
536
+ createSuborgData = _objectSpread2({
537
+ email: oidcEmail,
538
+ oauthProviders: oauthProviders
539
+ }, customAccounts && {
540
+ customAccounts: customAccounts
541
+ });
542
+ if (!(oidcEmail && iss === "https://accounts.google.com")) {
543
+ _context7.next = 81;
544
+ break;
545
+ }
546
+ _context7.next = 37;
547
+ return server.getVerifiedSuborgs({
548
+ filterType: FilterType.Email,
549
+ filterValue: oidcEmail
550
+ });
551
+ case 37:
552
+ _context7.t4 = _yield$server$getVeri = _context7.sent;
553
+ _context7.t3 = _context7.t4 === null;
554
+ if (_context7.t3) {
555
+ _context7.next = 41;
556
+ break;
557
+ }
558
+ _context7.t3 = _yield$server$getVeri === void 0;
559
+ case 41:
560
+ if (!_context7.t3) {
561
+ _context7.next = 45;
562
+ break;
563
+ }
564
+ _context7.t5 = void 0;
565
+ _context7.next = 46;
566
+ break;
567
+ case 45:
568
+ _context7.t5 = _yield$server$getVeri.organizationIds;
569
+ case 46:
570
+ emailOrgIds = _context7.t5;
571
+ if (!(emailOrgIds !== null && emailOrgIds !== void 0 && emailOrgIds[0])) {
572
+ _context7.next = 67;
573
+ break;
574
+ }
575
+ orgId = emailOrgIds[0];
576
+ // attach this new OIDC provider to the existing user
577
+ _context7.next = 51;
578
+ return server.getUsers({
579
+ organizationId: orgId
580
+ });
581
+ case 51:
582
+ _context7.t7 = _yield$server$getUser = _context7.sent;
583
+ _context7.t6 = _context7.t7 === null;
584
+ if (_context7.t6) {
585
+ _context7.next = 55;
586
+ break;
587
+ }
588
+ _context7.t6 = _yield$server$getUser === void 0;
589
+ case 55:
590
+ if (!_context7.t6) {
591
+ _context7.next = 59;
592
+ break;
593
+ }
594
+ _context7.t8 = void 0;
595
+ _context7.next = 60;
596
+ break;
597
+ case 59:
598
+ _context7.t8 = (_yield$server$getUser2 = _yield$server$getUser.users) === null || _yield$server$getUser2 === void 0 ? void 0 : (_yield$server$getUser3 = _yield$server$getUser2[0]) === null || _yield$server$getUser3 === void 0 ? void 0 : _yield$server$getUser3.userId;
599
+ case 60:
600
+ userId = _context7.t8;
601
+ if (userId) {
602
+ _context7.next = 63;
603
+ break;
604
+ }
605
+ return _context7.abrupt("return", onError(authErrors.oauth.loginFailed));
606
+ case 63:
607
+ _context7.next = 65;
608
+ return server.createOauthProviders({
609
+ organizationId: orgId,
610
+ userId: userId,
611
+ oauthProviders: oauthProviders
612
+ });
613
+ case 65:
614
+ _context7.next = 79;
615
+ break;
616
+ case 67:
617
+ _context7.next = 69;
618
+ return server.createSuborg(createSuborgData);
619
+ case 69:
620
+ _context7.t10 = _yield$server$createS = _context7.sent;
621
+ _context7.t9 = _context7.t10 === null;
622
+ if (_context7.t9) {
623
+ _context7.next = 73;
624
+ break;
625
+ }
626
+ _context7.t9 = _yield$server$createS === void 0;
627
+ case 73:
628
+ if (!_context7.t9) {
629
+ _context7.next = 77;
630
+ break;
631
+ }
632
+ _context7.t11 = void 0;
633
+ _context7.next = 78;
634
+ break;
635
+ case 77:
636
+ _context7.t11 = _yield$server$createS.subOrganizationId;
637
+ case 78:
638
+ orgId = _context7.t11;
639
+ case 79:
640
+ _context7.next = 93;
641
+ break;
642
+ case 81:
643
+ _context7.next = 83;
644
+ return server.createSuborg(createSuborgData);
645
+ case 83:
646
+ _context7.t13 = _yield$server$createS2 = _context7.sent;
647
+ _context7.t12 = _context7.t13 === null;
648
+ if (_context7.t12) {
649
+ _context7.next = 87;
650
+ break;
651
+ }
652
+ _context7.t12 = _yield$server$createS2 === void 0;
653
+ case 87:
654
+ if (!_context7.t12) {
655
+ _context7.next = 91;
656
+ break;
657
+ }
658
+ _context7.t14 = void 0;
659
+ _context7.next = 92;
660
+ break;
661
+ case 91:
662
+ _context7.t14 = _yield$server$createS2.subOrganizationId;
663
+ case 92:
664
+ orgId = _context7.t14;
665
+ case 93:
666
+ return _context7.abrupt("return", orgId ? completeLogin(orgId) : onError(authErrors.oauth.loginFailed));
667
+ case 94:
467
668
  case "end":
468
- return _context6.stop();
669
+ return _context7.stop();
469
670
  }
470
- }, _callee6);
671
+ }, _callee7);
471
672
  }));
472
673
  return function handleOAuthLogin(_x4, _x5) {
473
674
  return _ref8.apply(this, arguments);
474
675
  };
475
676
  }();
476
677
  var handleLoginWithWallet = /*#__PURE__*/function () {
477
- var _ref9 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7() {
678
+ var _ref11 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8() {
478
679
  var _authConfig$sessionLe3, publicKey, _walletClient$getWall, type, resp, suborgIds, pubKey;
479
- return _regeneratorRuntime().wrap(function _callee7$(_context7) {
480
- while (1) switch (_context7.prev = _context7.next) {
680
+ return _regeneratorRuntime().wrap(function _callee8$(_context8) {
681
+ while (1) switch (_context8.prev = _context8.next) {
481
682
  case 0:
482
683
  setLoading("wallet");
483
- _context7.prev = 1;
684
+ _context8.prev = 1;
484
685
  if (walletClient) {
485
- _context7.next = 4;
686
+ _context8.next = 4;
486
687
  break;
487
688
  }
488
689
  throw new Error("Wallet client not initialized");
489
690
  case 4:
490
- _context7.next = 6;
691
+ _context8.next = 6;
491
692
  return walletClient.getPublicKey();
492
693
  case 6:
493
- publicKey = _context7.sent;
694
+ publicKey = _context8.sent;
494
695
  if (publicKey) {
495
- _context7.next = 9;
696
+ _context8.next = 9;
496
697
  break;
497
698
  }
498
699
  throw new Error(authErrors.wallet.noPublicKey);
499
700
  case 9:
500
701
  _walletClient$getWall = walletClient.getWalletInterface(), type = _walletClient$getWall.type;
501
- _context7.next = 12;
702
+ _context8.next = 12;
502
703
  return server.getOrCreateSuborg({
503
704
  filterType: FilterType.PublicKey,
504
705
  filterValue: publicKey,
@@ -510,51 +711,51 @@ var Auth = function Auth(_ref) {
510
711
  }
511
712
  });
512
713
  case 12:
513
- resp = _context7.sent;
714
+ resp = _context8.sent;
514
715
  suborgIds = resp === null || resp === void 0 ? void 0 : resp.subOrganizationIds;
515
716
  if (!(!suborgIds || suborgIds.length === 0)) {
516
- _context7.next = 17;
717
+ _context8.next = 17;
517
718
  break;
518
719
  }
519
720
  onError(authErrors.wallet.loginFailed);
520
- return _context7.abrupt("return");
721
+ return _context8.abrupt("return");
521
722
  case 17:
522
- _context7.next = 19;
723
+ _context8.next = 19;
523
724
  return indexedDbClient.resetKeyPair();
524
725
  case 19:
525
- _context7.next = 21;
726
+ _context8.next = 21;
526
727
  return indexedDbClient.getPublicKey();
527
728
  case 21:
528
- pubKey = _context7.sent;
529
- _context7.next = 24;
729
+ pubKey = _context8.sent;
730
+ _context8.next = 24;
530
731
  return walletClient.loginWithWallet({
531
732
  sessionType: SessionType.READ_WRITE,
532
733
  publicKey: pubKey,
533
734
  expirationSeconds: (_authConfig$sessionLe3 = authConfig.sessionLengthSeconds) === null || _authConfig$sessionLe3 === void 0 ? void 0 : _authConfig$sessionLe3.toString()
534
735
  });
535
736
  case 24:
536
- _context7.next = 26;
737
+ _context8.next = 26;
537
738
  return onAuthSuccess();
538
739
  case 26:
539
- _context7.next = 32;
740
+ _context8.next = 32;
540
741
  break;
541
742
  case 28:
542
- _context7.prev = 28;
543
- _context7.t0 = _context7["catch"](1);
544
- onError(_context7.t0.message || authErrors.wallet.loginFailed);
545
- console.error("Error during wallet login: ", _context7.t0);
743
+ _context8.prev = 28;
744
+ _context8.t0 = _context8["catch"](1);
745
+ onError(_context8.t0.message || authErrors.wallet.loginFailed);
746
+ console.error("Error during wallet login: ", _context8.t0);
546
747
  case 32:
547
- _context7.prev = 32;
748
+ _context8.prev = 32;
548
749
  setLoading(undefined);
549
- return _context7.finish(32);
750
+ return _context8.finish(32);
550
751
  case 35:
551
752
  case "end":
552
- return _context7.stop();
753
+ return _context8.stop();
553
754
  }
554
- }, _callee7, null, [[1, 28, 32, 35]]);
755
+ }, _callee8, null, [[1, 28, 32, 35]]);
555
756
  }));
556
757
  return function handleLoginWithWallet() {
557
- return _ref9.apply(this, arguments);
758
+ return _ref11.apply(this, arguments);
558
759
  };
559
760
  }();
560
761
  var renderBackButton = function renderBackButton() {
@@ -592,7 +793,7 @@ var Auth = function Auth(_ref) {
592
793
  clientId: (_authConfig$googleCli = authConfig.googleClientId) !== null && _authConfig$googleCli !== void 0 ? _authConfig$googleCli : process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID,
593
794
  openInPage: authConfig.openOAuthInPage,
594
795
  onSuccess: function onSuccess(response) {
595
- return handleOAuthLogin(response.idToken, "Google");
796
+ return handleOAuthLogin(response.idToken, "Google", authConfig.socialLinking);
596
797
  }
597
798
  }), appleEnabled && jsx(AppleAuthButton, {
598
799
  layout: layout,
@@ -600,14 +801,14 @@ var Auth = function Auth(_ref) {
600
801
  openInPage: authConfig.openOAuthInPage,
601
802
  onSuccess: function onSuccess(response) {
602
803
  return handleOAuthLogin(response.idToken, "Apple");
603
- }
804
+ } // No need to pass socialLinking for Apple. It's not supported
604
805
  }), facebookEnabled && jsx(FacebookAuthButton, {
605
806
  layout: layout,
606
807
  clientId: (_authConfig$facebookC = authConfig.facebookClientId) !== null && _authConfig$facebookC !== void 0 ? _authConfig$facebookC : process.env.NEXT_PUBLIC_FACEBOOK_CLIENT_ID,
607
808
  openInPage: authConfig.openOAuthInPage,
608
809
  onSuccess: function onSuccess(response) {
609
810
  return handleOAuthLogin(response.id_token, "Facebook");
610
- }
811
+ } // No need to pass socialLinking for Facebook. It's not supported
611
812
  })]
612
813
  });
613
814
  };