Skip to content

Commit 64156ca

Browse files
author
mt-roberto
committed
feat(index): new logout endpoint
1 parent a38ef0e commit 64156ca

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

src/__tests__/index.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,66 @@ describe('LinkSDK', () => {
168168
});
169169
});
170170

171+
describe('logout', () => {
172+
test('Calling "logout" method before an init will fail', async () => {
173+
expect(() => {
174+
linkSDK.logout();
175+
}).toThrow('SDK not initialized');
176+
});
177+
178+
test('default params', async () => {
179+
const open = (window.open = jest.fn());
180+
181+
linkSDK.init.call(mockValue, {
182+
clientId: value
183+
});
184+
// @ts-ignore Ignores missing arguments to test user passing no arguments
185+
linkSDK.logout.call(mockValue);
186+
187+
expect(open).toBeCalled();
188+
189+
const [[url, isNewTab]] = open.mock.calls; // [0][1]
190+
191+
const host = `https://${mockValue.domains.myaccount}/${MY_ACCOUNT.PATHS.LOGOUT}`;
192+
expect(url).toContain(host);
193+
expect(isNewTab).toBe('_self');
194+
195+
const { params, oauthParams } = mockValue;
196+
197+
const configs = encodeURIComponent(`sdk_platform=js;sdk_version=${packageJSON.version}`);
198+
const qs =
199+
`client_id=${params.client_id}&redirect_uri=${encodeURIComponent(oauthParams.redirect_uri)}` +
200+
`&response_type=token&configs=${configs}`;
201+
expect(url).toBe(`${host}?${qs}`);
202+
});
203+
204+
test('with params', async () => {
205+
const open = (window.open = jest.fn());
206+
207+
linkSDK.init.call(mockValue, {
208+
clientId: value,
209+
scope: [value]
210+
});
211+
linkSDK.logout.call(mockValue, {
212+
newTab: true
213+
});
214+
215+
expect(open).toBeCalled();
216+
217+
const [[url, isNewTab]] = open.mock.calls; // [0][1]
218+
expect(isNewTab).toBe('_blank');
219+
220+
const { params, domains, oauthParams } = mockValue;
221+
const host = `https://${domains.myaccount}/${MY_ACCOUNT.PATHS.LOGOUT}`;
222+
const qs =
223+
`client_id=${params.client_id}&redirect_uri=${encodeURIComponent(oauthParams.redirect_uri)}` +
224+
`&response_type=token&scope=${value}`;
225+
const configs = encodeURIComponent(`sdk_platform=js;sdk_version=${packageJSON.version}`);
226+
227+
expect(url).toBe(`${host}?${qs}&configs=${configs}`);
228+
});
229+
});
230+
171231
describe('openVault', () => {
172232
test('Calling "openVault" method before an init will fail', async () => {
173233
expect(() => {

src/endpoints.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const MY_ACCOUNT = {
1010
TEST_SUBDOMAIN: 'myaccount-staging',
1111
PATHS: {
1212
OAUTH: 'oauth/authorize',
13-
SETTINGS: 'settings'
13+
SETTINGS: 'settings',
14+
LOGOUT: 'guests/logout'
1415
}
1516
};

src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,20 @@ class LinkSDK {
127127
window.open(`https://${this.domains.myaccount}/${MY_ACCOUNT.PATHS.OAUTH}${params}`, newTab ? '_blank' : '_self');
128128
}
129129

130+
// Open My Account and logs you out from the current session
131+
public logout({ newTab = false }: IMyAccountOptions = {}): void {
132+
if (!this.isInitialized) {
133+
throw new Error('SDK not initialized');
134+
}
135+
136+
const params = encodeConfigWithParams<IParams | IOauthParams, ICommonUrlConfig & IUrlConfig>(
137+
{ ...this.oauthParams, ...this.params },
138+
{ ...commonUrlConfig }
139+
);
140+
141+
window.open(`https://${this.domains.myaccount}/${MY_ACCOUNT.PATHS.LOGOUT}${params}`, newTab ? '_blank' : '_self');
142+
}
143+
130144
// Open the Vault page
131145
public openVault({ newTab = false, backTo = location.href }: IVaultOptions = {}): void {
132146
if (!this.isInitialized) {

0 commit comments

Comments
 (0)