@turnkey/sdk-react 5.0.2 → 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';
@@ -18,12 +19,8 @@ import { useTurnkey } from '../../hooks/use-turnkey.mjs';
18
19
  import { OtpType, FilterType, authErrors } from './constants.mjs';
19
20
  import { server } from '@turnkey/sdk-server';
20
21
  import parsePhoneNumberFromString from 'libphonenumber-js';
22
+ import { SessionType } from '@turnkey/sdk-types';
21
23
 
22
- var SessionType;
23
- (function (SessionType) {
24
- SessionType["READ_ONLY"] = "SESSION_TYPE_READ_ONLY";
25
- SessionType["READ_WRITE"] = "SESSION_TYPE_READ_WRITE";
26
- })(SessionType || (SessionType = {}));
27
24
  var passkeyIcon = jsxs("svg", {
28
25
  xmlns: "http://www.w3.org/2000/svg",
29
26
  width: "43",
@@ -402,107 +399,307 @@ var Auth = function Auth(_ref) {
402
399
  };
403
400
  }();
404
401
  var handleOAuthLogin = /*#__PURE__*/function () {
405
- var _ref8 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(credential, providerName) {
406
- var pubKey, createSuborgData, resp, suborgIds, suborgId, sessionResponse;
407
- return _regeneratorRuntime().wrap(function _callee6$(_context6) {
408
- 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) {
409
427
  case 0:
410
- _context6.next = 2;
428
+ socialLinking = _args7.length > 2 && _args7[2] !== undefined ? _args7[2] : false;
429
+ _context7.next = 3;
411
430
  return indexedDbClient === null || indexedDbClient === void 0 ? void 0 : indexedDbClient.getPublicKey();
412
- case 2:
413
- pubKey = _context6.sent;
431
+ case 3:
432
+ pubKey = _context7.sent;
414
433
  if (pubKey) {
415
- _context6.next = 5;
434
+ _context7.next = 6;
416
435
  break;
417
436
  }
418
- return _context6.abrupt("return");
419
- case 5:
437
+ return _context7.abrupt("return");
438
+ case 6:
420
439
  setOauthLoading(providerName);
421
- createSuborgData = {
422
- oauthProviders: [{
423
- providerName: providerName,
424
- oidcToken: credential
425
- }]
426
- };
427
- if (customAccounts) {
428
- 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;
429
487
  }
430
- _context6.next = 10;
488
+ _context7.next = 13;
431
489
  return server.getOrCreateSuborg({
432
490
  filterType: FilterType.OidcToken,
433
491
  filterValue: credential,
434
492
  additionalData: createSuborgData
435
493
  });
436
- case 10:
437
- resp = _context6.sent;
494
+ case 13:
495
+ resp = _context7.sent;
438
496
  suborgIds = resp === null || resp === void 0 ? void 0 : resp.subOrganizationIds;
439
- if (!(!suborgIds || suborgIds.length === 0)) {
440
- _context6.next = 15;
497
+ if (suborgIds !== null && suborgIds !== void 0 && suborgIds.length) {
498
+ _context7.next = 17;
441
499
  break;
442
500
  }
443
- onError(authErrors.oauth.loginFailed);
444
- return _context6.abrupt("return");
445
- case 15:
446
- suborgId = suborgIds[0];
447
- _context6.next = 18;
448
- return server.oauthLogin({
449
- suborgID: suborgId,
450
- oidcToken: credential,
451
- publicKey: pubKey,
452
- sessionLengthSeconds: authConfig.sessionLengthSeconds
453
- });
501
+ return _context7.abrupt("return", onError(authErrors.oauth.loginFailed));
502
+ case 17:
503
+ return _context7.abrupt("return", completeLogin(suborgIds[0]));
454
504
  case 18:
455
- sessionResponse = _context6.sent;
456
- if (!(sessionResponse && sessionResponse.session)) {
457
- _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;
458
515
  break;
459
516
  }
460
- _context6.next = 22;
461
- return indexedDbClient.loginWithSession(sessionResponse.session);
462
- case 22:
463
- _context6.next = 24;
464
- return onAuthSuccess();
517
+ _context7.t0 = _yield$server$getSubo === void 0;
465
518
  case 24:
466
- _context6.next = 27;
519
+ if (!_context7.t0) {
520
+ _context7.next = 28;
521
+ break;
522
+ }
523
+ _context7.t2 = void 0;
524
+ _context7.next = 29;
467
525
  break;
468
- case 26:
469
- onError(authErrors.oauth.loginFailed);
470
- 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:
471
668
  case "end":
472
- return _context6.stop();
669
+ return _context7.stop();
473
670
  }
474
- }, _callee6);
671
+ }, _callee7);
475
672
  }));
476
673
  return function handleOAuthLogin(_x4, _x5) {
477
674
  return _ref8.apply(this, arguments);
478
675
  };
479
676
  }();
480
677
  var handleLoginWithWallet = /*#__PURE__*/function () {
481
- var _ref9 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7() {
678
+ var _ref11 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8() {
482
679
  var _authConfig$sessionLe3, publicKey, _walletClient$getWall, type, resp, suborgIds, pubKey;
483
- return _regeneratorRuntime().wrap(function _callee7$(_context7) {
484
- while (1) switch (_context7.prev = _context7.next) {
680
+ return _regeneratorRuntime().wrap(function _callee8$(_context8) {
681
+ while (1) switch (_context8.prev = _context8.next) {
485
682
  case 0:
486
683
  setLoading("wallet");
487
- _context7.prev = 1;
684
+ _context8.prev = 1;
488
685
  if (walletClient) {
489
- _context7.next = 4;
686
+ _context8.next = 4;
490
687
  break;
491
688
  }
492
689
  throw new Error("Wallet client not initialized");
493
690
  case 4:
494
- _context7.next = 6;
691
+ _context8.next = 6;
495
692
  return walletClient.getPublicKey();
496
693
  case 6:
497
- publicKey = _context7.sent;
694
+ publicKey = _context8.sent;
498
695
  if (publicKey) {
499
- _context7.next = 9;
696
+ _context8.next = 9;
500
697
  break;
501
698
  }
502
699
  throw new Error(authErrors.wallet.noPublicKey);
503
700
  case 9:
504
701
  _walletClient$getWall = walletClient.getWalletInterface(), type = _walletClient$getWall.type;
505
- _context7.next = 12;
702
+ _context8.next = 12;
506
703
  return server.getOrCreateSuborg({
507
704
  filterType: FilterType.PublicKey,
508
705
  filterValue: publicKey,
@@ -514,51 +711,51 @@ var Auth = function Auth(_ref) {
514
711
  }
515
712
  });
516
713
  case 12:
517
- resp = _context7.sent;
714
+ resp = _context8.sent;
518
715
  suborgIds = resp === null || resp === void 0 ? void 0 : resp.subOrganizationIds;
519
716
  if (!(!suborgIds || suborgIds.length === 0)) {
520
- _context7.next = 17;
717
+ _context8.next = 17;
521
718
  break;
522
719
  }
523
720
  onError(authErrors.wallet.loginFailed);
524
- return _context7.abrupt("return");
721
+ return _context8.abrupt("return");
525
722
  case 17:
526
- _context7.next = 19;
723
+ _context8.next = 19;
527
724
  return indexedDbClient.resetKeyPair();
528
725
  case 19:
529
- _context7.next = 21;
726
+ _context8.next = 21;
530
727
  return indexedDbClient.getPublicKey();
531
728
  case 21:
532
- pubKey = _context7.sent;
533
- _context7.next = 24;
729
+ pubKey = _context8.sent;
730
+ _context8.next = 24;
534
731
  return walletClient.loginWithWallet({
535
732
  sessionType: SessionType.READ_WRITE,
536
733
  publicKey: pubKey,
537
734
  expirationSeconds: (_authConfig$sessionLe3 = authConfig.sessionLengthSeconds) === null || _authConfig$sessionLe3 === void 0 ? void 0 : _authConfig$sessionLe3.toString()
538
735
  });
539
736
  case 24:
540
- _context7.next = 26;
737
+ _context8.next = 26;
541
738
  return onAuthSuccess();
542
739
  case 26:
543
- _context7.next = 32;
740
+ _context8.next = 32;
544
741
  break;
545
742
  case 28:
546
- _context7.prev = 28;
547
- _context7.t0 = _context7["catch"](1);
548
- onError(_context7.t0.message || authErrors.wallet.loginFailed);
549
- 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);
550
747
  case 32:
551
- _context7.prev = 32;
748
+ _context8.prev = 32;
552
749
  setLoading(undefined);
553
- return _context7.finish(32);
750
+ return _context8.finish(32);
554
751
  case 35:
555
752
  case "end":
556
- return _context7.stop();
753
+ return _context8.stop();
557
754
  }
558
- }, _callee7, null, [[1, 28, 32, 35]]);
755
+ }, _callee8, null, [[1, 28, 32, 35]]);
559
756
  }));
560
757
  return function handleLoginWithWallet() {
561
- return _ref9.apply(this, arguments);
758
+ return _ref11.apply(this, arguments);
562
759
  };
563
760
  }();
564
761
  var renderBackButton = function renderBackButton() {
@@ -596,7 +793,7 @@ var Auth = function Auth(_ref) {
596
793
  clientId: (_authConfig$googleCli = authConfig.googleClientId) !== null && _authConfig$googleCli !== void 0 ? _authConfig$googleCli : process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID,
597
794
  openInPage: authConfig.openOAuthInPage,
598
795
  onSuccess: function onSuccess(response) {
599
- return handleOAuthLogin(response.idToken, "Google");
796
+ return handleOAuthLogin(response.idToken, "Google", authConfig.socialLinking);
600
797
  }
601
798
  }), appleEnabled && jsx(AppleAuthButton, {
602
799
  layout: layout,
@@ -604,14 +801,14 @@ var Auth = function Auth(_ref) {
604
801
  openInPage: authConfig.openOAuthInPage,
605
802
  onSuccess: function onSuccess(response) {
606
803
  return handleOAuthLogin(response.idToken, "Apple");
607
- }
804
+ } // No need to pass socialLinking for Apple. It's not supported
608
805
  }), facebookEnabled && jsx(FacebookAuthButton, {
609
806
  layout: layout,
610
807
  clientId: (_authConfig$facebookC = authConfig.facebookClientId) !== null && _authConfig$facebookC !== void 0 ? _authConfig$facebookC : process.env.NEXT_PUBLIC_FACEBOOK_CLIENT_ID,
611
808
  openInPage: authConfig.openOAuthInPage,
612
809
  onSuccess: function onSuccess(response) {
613
810
  return handleOAuthLogin(response.id_token, "Facebook");
614
- }
811
+ } // No need to pass socialLinking for Facebook. It's not supported
615
812
  })]
616
813
  });
617
814
  };
@@ -882,5 +1079,5 @@ var Auth = function Auth(_ref) {
882
1079
  });
883
1080
  };
884
1081
 
885
- export { SessionType, Auth as default };
1082
+ export { Auth as default };
886
1083
  //# sourceMappingURL=Auth.mjs.map