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

Ensure to only clear current client cache when logging out #1068

Merged
merged 2 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 33 additions & 1 deletion __tests__/Auth0Client/logout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe('Auth0Client', () => {
);
});

it('clears the cache', async () => {
it('clears the cache for the global clientId', async () => {
const auth0 = setup();

jest
Expand All @@ -141,7 +141,39 @@ describe('Auth0Client', () => {

await auth0.logout();

expect(auth0['cacheManager']['clear']).toHaveBeenCalledWith(
TEST_CLIENT_ID
);
});

it('clears the cache for the provided clientId', async () => {
const auth0 = setup();

jest
.spyOn(auth0['cacheManager'], 'clear')
.mockReturnValueOnce(Promise.resolve());

await auth0.logout({ clientId: 'client_123' });

expect(auth0['cacheManager']['clear']).toHaveBeenCalledWith('client_123');
expect(auth0['cacheManager']['clear']).not.toHaveBeenCalledWith(
TEST_CLIENT_ID
);
});

it('clears the cache for all client ids', async () => {
const auth0 = setup();

jest
.spyOn(auth0['cacheManager'], 'clear')
.mockReturnValueOnce(Promise.resolve());

await auth0.logout({ clientId: null });

expect(auth0['cacheManager']['clear']).toHaveBeenCalled();
expect(auth0['cacheManager']['clear']).not.toHaveBeenCalledWith(
TEST_CLIENT_ID
);
});

it('removes authenticated cookie from storage when `options.onRedirect` is set', async () => {
Expand Down
6 changes: 5 additions & 1 deletion src/Auth0Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,11 @@ export class Auth0Client {
public async logout(options: LogoutOptions = {}): Promise<void> {
const { openUrl, ...logoutOptions } = patchOpenUrlWithOnRedirect(options);

await this.cacheManager.clear();
if (options.clientId === null) {
await this.cacheManager.clear();
} else {
await this.cacheManager.clear(options.clientId || this.options.clientId);
}

this.cookieStorage.remove(this.orgHintCookieName, {
cookieDomain: this.options.cookieDomain
Expand Down
2 changes: 1 addition & 1 deletion src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ export interface LogoutUrlOptions {
*
* [Read more about how redirecting after logout works](https://auth0.com/docs/logout/guides/redirect-users-after-logout)
*/
clientId?: string;
clientId?: string | null;

/**
* Parameters to pass to the logout endpoint. This can be known parameters defined by Auth0 or custom parameters
Expand Down