Skip to content

Commit

Permalink
feat(index): new logout endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mt-roberto committed Dec 10, 2019
1 parent a38ef0e commit 64156ca
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
60 changes: 60 additions & 0 deletions src/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,66 @@ describe('LinkSDK', () => {
});
});

describe('logout', () => {
test('Calling "logout" method before an init will fail', async () => {
expect(() => {
linkSDK.logout();
}).toThrow('SDK not initialized');
});

test('default params', async () => {
const open = (window.open = jest.fn());

linkSDK.init.call(mockValue, {
clientId: value
});
// @ts-ignore Ignores missing arguments to test user passing no arguments
linkSDK.logout.call(mockValue);

expect(open).toBeCalled();

const [[url, isNewTab]] = open.mock.calls; // [0][1]

const host = `https://${mockValue.domains.myaccount}/${MY_ACCOUNT.PATHS.LOGOUT}`;
expect(url).toContain(host);
expect(isNewTab).toBe('_self');

const { params, oauthParams } = mockValue;

const configs = encodeURIComponent(`sdk_platform=js;sdk_version=${packageJSON.version}`);
const qs =
`client_id=${params.client_id}&redirect_uri=${encodeURIComponent(oauthParams.redirect_uri)}` +
`&response_type=token&configs=${configs}`;
expect(url).toBe(`${host}?${qs}`);
});

test('with params', async () => {
const open = (window.open = jest.fn());

linkSDK.init.call(mockValue, {
clientId: value,
scope: [value]
});
linkSDK.logout.call(mockValue, {
newTab: true
});

expect(open).toBeCalled();

const [[url, isNewTab]] = open.mock.calls; // [0][1]
expect(isNewTab).toBe('_blank');

const { params, domains, oauthParams } = mockValue;
const host = `https://${domains.myaccount}/${MY_ACCOUNT.PATHS.LOGOUT}`;
const qs =
`client_id=${params.client_id}&redirect_uri=${encodeURIComponent(oauthParams.redirect_uri)}` +
`&response_type=token&scope=${value}`;
const configs = encodeURIComponent(`sdk_platform=js;sdk_version=${packageJSON.version}`);

expect(url).toBe(`${host}?${qs}&configs=${configs}`);
});
});

describe('openVault', () => {
test('Calling "openVault" method before an init will fail', async () => {
expect(() => {
Expand Down
3 changes: 2 additions & 1 deletion src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const MY_ACCOUNT = {
TEST_SUBDOMAIN: 'myaccount-staging',
PATHS: {
OAUTH: 'oauth/authorize',
SETTINGS: 'settings'
SETTINGS: 'settings',
LOGOUT: 'guests/logout'
}
};
14 changes: 14 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ class LinkSDK {
window.open(`https://${this.domains.myaccount}/${MY_ACCOUNT.PATHS.OAUTH}${params}`, newTab ? '_blank' : '_self');
}

// Open My Account and logs you out from the current session
public logout({ newTab = false }: IMyAccountOptions = {}): void {
if (!this.isInitialized) {
throw new Error('SDK not initialized');
}

const params = encodeConfigWithParams<IParams | IOauthParams, ICommonUrlConfig & IUrlConfig>(
{ ...this.oauthParams, ...this.params },
{ ...commonUrlConfig }
);

window.open(`https://${this.domains.myaccount}/${MY_ACCOUNT.PATHS.LOGOUT}${params}`, newTab ? '_blank' : '_self');
}

// Open the Vault page
public openVault({ newTab = false, backTo = location.href }: IVaultOptions = {}): void {
if (!this.isInitialized) {
Expand Down

0 comments on commit 64156ca

Please sign in to comment.