jazz-tools 0.10.12 → 0.10.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.
@@ -1,5 +1,5 @@
1
1
 
2
- > jazz-tools@0.10.12 build /home/runner/_work/jazz/jazz/packages/jazz-tools
2
+ > jazz-tools@0.10.14 build /home/runner/_work/jazz/jazz/packages/jazz-tools
3
3
  > tsup
4
4
 
5
5
  CLI Building entry: {"index":"src/index.ts","testing":"src/testing.ts"}
@@ -11,8 +11,8 @@
11
11
  ESM Build start
12
12
  ESM dist/index.js 1.50 KB
13
13
  ESM dist/testing.js 6.28 KB
14
- ESM dist/chunk-RL7HVQ5Q.js 113.88 KB
14
+ ESM dist/chunk-5YDDEUNX.js 115.67 KB
15
15
  ESM dist/index.js.map 259.00 B
16
16
  ESM dist/testing.js.map 12.41 KB
17
- ESM dist/chunk-RL7HVQ5Q.js.map 272.98 KB
18
- ESM ⚡️ Build success in 42ms
17
+ ESM dist/chunk-5YDDEUNX.js.map 275.90 KB
18
+ ESM ⚡️ Build success in 38ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # jazz-tools
2
2
 
3
+ ## 0.10.14
4
+
5
+ ### Patch Changes
6
+
7
+ - 75211e3: Fixes invalid authentication state when logging out after signUp
8
+
9
+ ## 0.10.13
10
+
11
+ ### Patch Changes
12
+
13
+ - 07feedd: Add registerNewUser and generateRandomPassphrase methods to PasskeyAuth and accept the username param on the signUp function
14
+
3
15
  ## 0.10.12
4
16
 
5
17
  ### Patch Changes
@@ -545,7 +545,7 @@ async function createJazzContext(options) {
545
545
  AccountSchema: options.AccountSchema,
546
546
  sessionProvider: options.sessionProvider,
547
547
  onLogOut: () => {
548
- authSecretStorage.clear();
548
+ authSecretStorage.clearWithoutNotify();
549
549
  }
550
550
  });
551
551
  } else {
@@ -561,11 +561,11 @@ async function createJazzContext(options) {
561
561
  crypto,
562
562
  AccountSchema: options.AccountSchema,
563
563
  onLogOut: async () => {
564
- await authSecretStorage.clear();
564
+ await authSecretStorage.clearWithoutNotify();
565
565
  }
566
566
  });
567
567
  if (!options.newAccountProps) {
568
- await authSecretStorage.set({
568
+ await authSecretStorage.setWithoutNotify({
569
569
  accountID: context.account.id,
570
570
  secretSeed,
571
571
  accountSecret: context.node.account.agentSecret,
@@ -3457,7 +3457,6 @@ var KvStoreContext_default = KvStoreContext;
3457
3457
  var STORAGE_KEY = "jazz-logged-in-secret";
3458
3458
  var AuthSecretStorage = class {
3459
3459
  constructor() {
3460
- this.notify = false;
3461
3460
  this.listeners = /* @__PURE__ */ new Set();
3462
3461
  this.isAuthenticated = false;
3463
3462
  }
@@ -3522,7 +3521,7 @@ var AuthSecretStorage = class {
3522
3521
  provider: parsed.provider
3523
3522
  };
3524
3523
  }
3525
- async set(payload) {
3524
+ async setWithoutNotify(payload) {
3526
3525
  const kvStore = KvStoreContext_default.getInstance().getStorage();
3527
3526
  await kvStore.set(
3528
3527
  STORAGE_KEY,
@@ -3533,9 +3532,10 @@ var AuthSecretStorage = class {
3533
3532
  provider: payload.provider
3534
3533
  })
3535
3534
  );
3536
- if (this.notify) {
3537
- this.emitUpdate(payload);
3538
- }
3535
+ }
3536
+ async set(payload) {
3537
+ this.setWithoutNotify(payload);
3538
+ this.emitUpdate(payload);
3539
3539
  }
3540
3540
  getIsAuthenticated(data) {
3541
3541
  if (!data) return false;
@@ -3555,12 +3555,13 @@ var AuthSecretStorage = class {
3555
3555
  listener(this.isAuthenticated);
3556
3556
  }
3557
3557
  }
3558
- async clear() {
3558
+ async clearWithoutNotify() {
3559
3559
  const kvStore = KvStoreContext_default.getInstance().getStorage();
3560
3560
  await kvStore.delete(STORAGE_KEY);
3561
- if (this.notify) {
3562
- this.emitUpdate(null);
3563
- }
3561
+ }
3562
+ async clear() {
3563
+ await this.clearWithoutNotify();
3564
+ this.emitUpdate(null);
3564
3565
  }
3565
3566
  };
3566
3567
 
@@ -3590,10 +3591,6 @@ import { cojsonInternals as cojsonInternals4 } from "cojson";
3590
3591
  var JazzContextManager = class {
3591
3592
  constructor() {
3592
3593
  this.authSecretStorage = new AuthSecretStorage();
3593
- this.authSecretStorageWithNotify = Object.assign(
3594
- Object.create(this.authSecretStorage),
3595
- { notify: true }
3596
- );
3597
3594
  this.authenticating = false;
3598
3595
  this.logOut = async () => {
3599
3596
  if (!this.context || !this.props) {
@@ -3623,28 +3620,33 @@ var JazzContextManager = class {
3623
3620
  await this.createContext(this.props, { credentials }).finally(() => {
3624
3621
  this.authenticating = false;
3625
3622
  });
3626
- const currentContext = this.context;
3627
- if (prevContext && currentContext && "me" in prevContext && "me" in currentContext && wasAnonymous) {
3628
- const [prevAccountAsPeer, currentAccountAsPeer] = cojsonInternals4.connectedPeers(
3629
- prevContext.me.id,
3630
- currentContext.me.id,
3631
- {
3632
- peer1role: "client",
3633
- peer2role: "server"
3634
- }
3635
- );
3636
- prevContext.node.syncManager.addPeer(currentAccountAsPeer);
3637
- currentContext.node.syncManager.addPeer(prevAccountAsPeer);
3638
- try {
3639
- await this.props.onAnonymousAccountDiscarded?.(prevContext.me);
3640
- await prevContext.me.waitForAllCoValuesSync();
3641
- } catch (error) {
3642
- console.error("Error onAnonymousAccountDiscarded", error);
3623
+ if (wasAnonymous) {
3624
+ await this.handleAnonymousAccountMigration(prevContext);
3625
+ }
3626
+ };
3627
+ this.register = async (accountSecret, creationProps) => {
3628
+ if (!this.props) {
3629
+ throw new Error("Props required");
3630
+ }
3631
+ const prevContext = this.context;
3632
+ const prevCredentials = await this.authSecretStorage.get();
3633
+ const wasAnonymous = this.authSecretStorage.getIsAuthenticated(prevCredentials) === false;
3634
+ this.authenticating = true;
3635
+ await this.createContext(this.props, {
3636
+ newAccountProps: {
3637
+ secret: accountSecret,
3638
+ creationProps
3643
3639
  }
3644
- prevAccountAsPeer.outgoing.close();
3645
- currentAccountAsPeer.outgoing.close();
3640
+ }).finally(() => {
3641
+ this.authenticating = false;
3642
+ });
3643
+ if (wasAnonymous) {
3644
+ await this.handleAnonymousAccountMigration(prevContext);
3645
+ }
3646
+ if (this.context && "me" in this.context) {
3647
+ return this.context.me.id;
3646
3648
  }
3647
- prevContext?.done();
3649
+ throw new Error("The registration hasn't created a new account");
3648
3650
  };
3649
3651
  this.listeners = /* @__PURE__ */ new Set();
3650
3652
  this.subscribe = (callback) => {
@@ -3673,6 +3675,7 @@ var JazzContextManager = class {
3673
3675
  ...context,
3674
3676
  node: context.node,
3675
3677
  authenticate: this.authenticate,
3678
+ register: this.register,
3676
3679
  logOut: this.logOut
3677
3680
  };
3678
3681
  if (authProps?.credentials) {
@@ -3690,7 +3693,34 @@ var JazzContextManager = class {
3690
3693
  return this.value;
3691
3694
  }
3692
3695
  getAuthSecretStorage() {
3693
- return this.authSecretStorageWithNotify;
3696
+ return this.authSecretStorage;
3697
+ }
3698
+ async handleAnonymousAccountMigration(prevContext) {
3699
+ if (!this.props) {
3700
+ throw new Error("Props required");
3701
+ }
3702
+ const currentContext = this.context;
3703
+ if (prevContext && currentContext && "me" in prevContext && "me" in currentContext) {
3704
+ const [prevAccountAsPeer, currentAccountAsPeer] = cojsonInternals4.connectedPeers(
3705
+ prevContext.me.id,
3706
+ currentContext.me.id,
3707
+ {
3708
+ peer1role: "client",
3709
+ peer2role: "server"
3710
+ }
3711
+ );
3712
+ prevContext.node.syncManager.addPeer(currentAccountAsPeer);
3713
+ currentContext.node.syncManager.addPeer(prevAccountAsPeer);
3714
+ try {
3715
+ await this.props.onAnonymousAccountDiscarded?.(prevContext.me);
3716
+ await prevContext.me.waitForAllCoValuesSync();
3717
+ } catch (error) {
3718
+ console.error("Error onAnonymousAccountDiscarded", error);
3719
+ }
3720
+ prevAccountAsPeer.outgoing.close();
3721
+ currentAccountAsPeer.outgoing.close();
3722
+ }
3723
+ prevContext?.done();
3694
3724
  }
3695
3725
  notify() {
3696
3726
  for (const listener of this.listeners) {
@@ -3825,9 +3855,10 @@ import * as bip39 from "@scure/bip39";
3825
3855
  import { entropyToMnemonic } from "@scure/bip39";
3826
3856
  import { cojsonInternals as cojsonInternals5 } from "cojson";
3827
3857
  var PassphraseAuth = class {
3828
- constructor(crypto, authenticate, authSecretStorage, wordlist) {
3858
+ constructor(crypto, authenticate, register, authSecretStorage, wordlist) {
3829
3859
  this.crypto = crypto;
3830
3860
  this.authenticate = authenticate;
3861
+ this.register = register;
3831
3862
  this.authSecretStorage = authSecretStorage;
3832
3863
  this.wordlist = wordlist;
3833
3864
  this.passphrase = "";
@@ -3857,7 +3888,7 @@ var PassphraseAuth = class {
3857
3888
  this.passphrase = passphrase;
3858
3889
  this.notify();
3859
3890
  };
3860
- this.signUp = async () => {
3891
+ this.signUp = async (name) => {
3861
3892
  const credentials = await this.authSecretStorage.get();
3862
3893
  if (!credentials || !credentials.secretSeed) {
3863
3894
  throw new Error("No credentials found");
@@ -3869,8 +3900,26 @@ var PassphraseAuth = class {
3869
3900
  accountSecret: credentials.accountSecret,
3870
3901
  provider: "passphrase"
3871
3902
  });
3903
+ if (name?.trim()) {
3904
+ const currentAccount = await Account.getMe().ensureLoaded({
3905
+ profile: {}
3906
+ });
3907
+ currentAccount.profile.name = name;
3908
+ }
3872
3909
  return passphrase;
3873
3910
  };
3911
+ this.registerNewAccount = async (passphrase, name) => {
3912
+ const secretSeed = bip39.mnemonicToEntropy(passphrase, this.wordlist);
3913
+ const accountSecret = this.crypto.agentSecretFromSecretSeed(secretSeed);
3914
+ const accountID = await this.register(accountSecret, { name });
3915
+ await this.authSecretStorage.set({
3916
+ accountID,
3917
+ secretSeed,
3918
+ accountSecret,
3919
+ provider: "passphrase"
3920
+ });
3921
+ return accountID;
3922
+ };
3874
3923
  this.getCurrentAccountPassphrase = async () => {
3875
3924
  const credentials = await this.authSecretStorage.get();
3876
3925
  if (!credentials || !credentials.secretSeed) {
@@ -3878,6 +3927,9 @@ var PassphraseAuth = class {
3878
3927
  }
3879
3928
  return entropyToMnemonic(credentials.secretSeed, this.wordlist);
3880
3929
  };
3930
+ this.generateRandomPassphrase = () => {
3931
+ return entropyToMnemonic(this.crypto.newRandomSecretSeed(), this.wordlist);
3932
+ };
3881
3933
  this.loadCurrentAccountPassphrase = async () => {
3882
3934
  const passphrase = await this.getCurrentAccountPassphrase();
3883
3935
  this.passphrase = passphrase;
@@ -3993,4 +4045,4 @@ export {
3993
4045
  consumeInviteLink
3994
4046
  };
3995
4047
  /* istanbul ignore file -- @preserve */
3996
- //# sourceMappingURL=chunk-RL7HVQ5Q.js.map
4048
+ //# sourceMappingURL=chunk-5YDDEUNX.js.map