Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: onAuthStateChange #82

Merged
merged 2 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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).toHaveBeenCalledWith(true);
});

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).toHaveBeenCalledWith(true);
});

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).toHaveBeenCalledWith(false);
});
});
});
15 changes: 14 additions & 1 deletion packages/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface ConfigParameters {
clientId: string;
scope?: string | string[];
storage?: ClientStorage;
onAuthStateChange?: (isAuthenticated: boolean) => void;
}

export const appendSlashIfNeeded = (url: string): string => {
Expand All @@ -40,10 +41,12 @@ export default class LogtoClient {
private readonly scope: string;
private readonly sessionManager: SessionManager;
private readonly storage: ClientStorage;
private readonly onAuthStateChange: Optional<(isAuthenticated: boolean) => 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 @@ -120,6 +123,9 @@ export default class LogtoClient {
);
this.storage.setItem(this.tokenSetCacheKey, tokenParameters);
this.tokenSet = new TokenSet(tokenParameters);
if (this.onAuthStateChange) {
this.onAuthStateChange(true);
}
}

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

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

if (!this.tokenSet) {
return;
}
Expand All @@ -182,6 +192,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(true);
}
}
}
}