Skip to content

Commit

Permalink
feat: onAuthStateChange (#82)
Browse files Browse the repository at this point in the history
* feat: onAuthStateChange

* fix: remove onAuthStateChange param to keep SSOT
  • Loading branch information
wangsijie authored Nov 4, 2021
1 parent efc2d01 commit e8dda56
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
48 changes: 48 additions & 0 deletions packages/client/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,52 @@ describe('LogtoClient', () => {
expect(storage.removeItem).toBeCalled();
});
});

describe('onAuthStateChange', () => {
test('should be called on tokenSet recovery', async () => {
const onAuthStateChange = jest.fn();
const storage = new MemoryStorage();
storage.setItem(
`LOGTO_TOKEN_SET_CACHE::${discoverResponse.issuer}::${CLIENT_ID}::${DEFAULT_SCOPE}`,
{
...fakeTokenResponse,
id_token: (await generateIdToken()).idToken,
}
);
await LogtoClient.create({
domain: DOMAIN,
clientId: CLIENT_ID,
storage,
onAuthStateChange,
});
expect(onAuthStateChange).toHaveBeenCalled();
});

test('should be called on handleCallback', async () => {
const onAuthStateChange = jest.fn();
const storage = new MemoryStorage();
const logto = await LogtoClient.create({
domain: DOMAIN,
clientId: CLIENT_ID,
storage,
onAuthStateChange,
});
await logto.loginWithRedirect(REDIRECT_URI);
await logto.handleCallback(REDIRECT_CALLBACK);
expect(onAuthStateChange).toHaveBeenCalled();
});

test('should be called on logout', async () => {
const onAuthStateChange = jest.fn();
const storage = new MemoryStorage();
const logto = await LogtoClient.create({
domain: DOMAIN,
clientId: CLIENT_ID,
storage,
onAuthStateChange,
});
logto.logout(REDIRECT_URI);
expect(onAuthStateChange).toHaveBeenCalled();
});
});
});
15 changes: 14 additions & 1 deletion packages/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface ConfigParameters {
clientId: string;
scope?: string | string[];
storage?: ClientStorage;
onAuthStateChange?: () => void;
}

export const appendSlashIfNeeded = (url: string): string => {
Expand All @@ -39,10 +40,12 @@ export default class LogtoClient {
private readonly scope: string;
private readonly sessionManager: SessionManager;
private readonly storage: ClientStorage;
private readonly onAuthStateChange: Optional<() => void>;
private tokenSet: Optional<TokenSet>;
constructor(config: ConfigParameters, oidcConfiguration: OIDCConfiguration) {
const { clientId, scope, storage } = config;
const { clientId, scope, storage, onAuthStateChange } = config;
this.clientId = clientId;
this.onAuthStateChange = onAuthStateChange;
this.scope = generateScope(scope);
this.oidcConfiguration = oidcConfiguration;
this.storage = storage ?? new LocalStorage();
Expand Down Expand Up @@ -119,6 +122,9 @@ export default class LogtoClient {
);
this.storage.setItem(this.tokenSetCacheKey, tokenParameters);
this.tokenSet = new TokenSet(tokenParameters);
if (this.onAuthStateChange) {
this.onAuthStateChange();
}
}

public getClaims() {
Expand Down Expand Up @@ -159,6 +165,10 @@ export default class LogtoClient {

public logout(redirectUri: string) {
this.sessionManager.clear();
if (this.onAuthStateChange) {
this.onAuthStateChange();
}

if (!this.tokenSet) {
return;
}
Expand All @@ -181,6 +191,9 @@ export default class LogtoClient {
const parameters = this.storage.getItem<TokenSetParameters>(this.tokenSetCacheKey);
if (parameters) {
this.tokenSet = new TokenSet(parameters);
if (this.onAuthStateChange) {
this.onAuthStateChange();
}
}
}
}

0 comments on commit e8dda56

Please sign in to comment.