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: Logout previous sessions RELEASE #846

Merged
merged 4 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions packages/sdks/core-js-sdk/src/constants/apiPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export default {
selectTenant: '/v1/auth/tenant/select',
logout: '/v1/auth/logout',
logoutAll: '/v1/auth/logoutall',
logoutPrevious: '/v1/auth/logoutprevious',
me: '/v1/auth/me',
myTenants: '/v1/auth/me/tenants',
history: '/v1/auth/me/history',
Expand Down
9 changes: 9 additions & 0 deletions packages/sdks/core-js-sdk/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ export default (httpClient: HttpClient) => ({
httpClient.post(apiPaths.logoutAll, {}, { token }),
),
),
/**
* Logs out all previous sessions for the current user
* @param token A valid refresh token
*/
logoutPrevious: withOptionalTokenValidations((token?: string) =>
transformResponse<never>(
httpClient.post(apiPaths.logoutPrevious, {}, { token }),
),
),
/**
* Returns the current user details
* @param token A valid refresh token
Expand Down
27 changes: 27 additions & 0 deletions packages/sdks/core-js-sdk/test/sdk/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,33 @@ describe('sdk', () => {
});
});

describe('logoutPrevious', () => {
it('should throw an error when token is not a string', () => {
expect(() => sdk.logoutPrevious({ a: 'b' })).toThrow(
'"token" must be string or undefined',
);
});
it('should send the correct request', () => {
const httpRespJson = { key: 'val' };
const httpResponse = {
ok: true,
json: () => httpRespJson,
clone: () => ({
json: () => Promise.resolve(httpRespJson),
}),
status: 200,
};
mockHttpClient.post.mockResolvedValue(httpResponse);

sdk.logoutPrevious('token');
expect(mockHttpClient.post).toHaveBeenCalledWith(
apiPaths.logoutPrevious,
{},
{ token: 'token' },
);
});
});

describe('me', () => {
it('should throw an error when token is not a string', () => {
expect(() => sdk.me({ a: 'b' })).toThrow(
Expand Down
3 changes: 2 additions & 1 deletion packages/sdks/react-sdk/test/hooks/useAuth.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jest.mock('@descope/web-js-sdk', () => {
const sdk = {
logout: jest.fn().mockName('logout'),
logoutAll: jest.fn().mockName('logoutAll'),
logoutPrevious: jest.fn().mockName('logoutPrevious'),
otp: {
signIn: {
email: jest.fn().mockName('otp.signIn.email'),
Expand Down Expand Up @@ -64,7 +65,7 @@ describe('hooks', () => {
);
});

it.each(['logoutAll', 'logout', 'otp.signIn.email'])(
it.each(['logoutAll', 'logoutPrevious', 'logout', 'otp.signIn.email'])(
'should throw error when using sdk function before sdk initialization - %s',
(fnName) => {
const { result } = renderHook(() => useDescope(), {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,5 @@ export const withAutoRefresh =
return resp;
};

return wrapWith(sdk, ['logout', 'logoutAll'], wrapper);
return wrapWith(sdk, ['logout', 'logoutAll', 'logoutPrevious'], wrapper);
};
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const withLastLoggedInUser =
let wrappedSdk = wrapWith(sdk, ['flow.start'], startWrapper);
wrappedSdk = wrapWith(
wrappedSdk,
['logout', 'logoutAll'],
['logout', 'logoutAll', 'logoutPrevious'],
logoutWrapper(keepLastAuthenticatedUserAfterLogout),
);
return Object.assign(wrappedSdk, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ export const withNotifications =
return resp;
};

const wrappedSdk = wrapWith(sdk, ['logout', 'logoutAll'], wrapper);
const wrappedSdk = wrapWith(
sdk,
['logout', 'logoutAll', 'logoutPrevious'],
wrapper,
);

return Object.assign(wrappedSdk, {
onSessionTokenChange: sessionPS.sub,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const withPersistTokens =

const wrappedSdk = wrapWith(
sdk,
['logout', 'logoutAll'],
['logout', 'logoutAll', 'logoutPrevious'],
wrapper(storagePrefix),
);

Expand Down
13 changes: 13 additions & 0 deletions packages/sdks/web-js-sdk/test/persistTokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,19 @@ describe('persistTokens', () => {
expect(removeMock).toBeCalledWith('DS');
});

it('should clear tokens on logoutPrevious even when not passing refresh token', async () => {
localStorage.setItem('DSR', authInfo.refreshJwt);
const mockFetch = jest.fn().mockReturnValue(createMockReturnValue({}));
global.fetch = mockFetch;

const sdk = createSdk({ projectId: 'pid', persistTokens: true });
await sdk.logoutPrevious();

expect(localStorage.getItem('DSR')).toBeFalsy();
const removeMock = Cookies.remove as jest.Mock;
expect(removeMock).toBeCalledWith('DS');
});

it('should not log a warning when not running in the browser', () => {
const warnSpy = jest.spyOn(console, 'warn');

Expand Down
Loading