Compare commits

...

2 Commits

4 changed files with 23 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
---
"jazz-tools": patch
---
Add onAuthChange lifecycle hook to Account

View File

@@ -104,6 +104,10 @@ export class MusicaAccount extends Account {
});
}
}
onAuthChange(isAuthenticated: boolean) {
console.log("onAuthChange", isAuthenticated, this.id);
}
}
/** Walkthrough: Continue with ./2_main.tsx */

View File

@@ -1,5 +1,6 @@
import { AgentSecret } from "cojson";
import type { Account } from "../coValues/account.js";
import { activeAccountContext } from "../implementation/activeAccountContext.js";
import type { ID } from "../internal.js";
import { AuthCredentials } from "../types.js";
import KvStoreContext from "./KvStoreContext.js";
@@ -112,7 +113,12 @@ export class AuthSecretStorage {
);
if (this.notify) {
this.emitUpdate(payload);
const authStateChanged = this.emitUpdate(payload);
const currentAccount = activeAccountContext.maybeGet();
if (currentAccount && authStateChanged) {
await currentAccount.onSignUp(this.isAuthenticated);
}
}
}
@@ -131,12 +137,14 @@ export class AuthSecretStorage {
emitUpdate(data: AuthCredentials | null) {
const isAuthenticated = this.getIsAuthenticated(data);
if (this.isAuthenticated === isAuthenticated) return;
if (this.isAuthenticated === isAuthenticated) return false;
this.isAuthenticated = isAuthenticated;
for (const listener of this.listeners) {
listener(this.isAuthenticated);
}
return true;
}
async clear() {

View File

@@ -341,6 +341,10 @@ export class Account extends CoValueBase implements CoValue {
creationProps; // To avoid unused parameter warning
}
onSignUp(isAuthenticated: boolean) {
isAuthenticated; // To avoid unused parameter warning
}
/** @category Subscription & Loading */
static load<A extends Account, Depth>(
this: CoValueClass<A>,