Skip to content

Commit

Permalink
fix(logout): add optional backTo parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
mt-max committed Jan 17, 2020
1 parent fcd7e31 commit 9b9da89
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ You can pass the following options:
- Values: `true` or `false`
- Default: `false`

- `backTo`: Redirect back to this url after logout, url must be whitelisted (contact support)
- Values: A string representing a URL
- Default: `undefined`

### Open the setting page of the user account

`mtLinkSdk.openSettings(options);`
Expand Down
9 changes: 8 additions & 1 deletion sample/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ <h3>Welcome to the Moneytree Link Web SDK Sample App</h3>
<button id="authorize-btn">Authorize</button>
<button id="settings-btn">Open Moneytree Settings</button>
<button id="vault-btn">Open Vault</button>
<button id="logout-btn">Logout</button>
<p>
Logout Url (optional), must be whitelisted
<input id="logout-url" type="text" placeholder="Optional logout url"/>

<div>
<button id="logout-btn">Logout</button>
</div>
</p>
</div>
</body>
</html>
8 changes: 6 additions & 2 deletions sample/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ authorizeBtn.onclick = () => {

// Launch logout route when clicked
logoutBtn.onclick = () => {
LinkSDK.logout();
const value = document.getElementById('logout-url').value;

LinkSDK.logout({
backTo: value ? value : undefined
});
};

// Launch settings route when clicked
Expand Down Expand Up @@ -62,7 +66,7 @@ const validateToken = async () => {
if (!accessToken) {
goToSettingsBtn.disabled = true;
goToVaultBtn.disabled = true;
logoutBtn.disabled = true;
// logoutBtn.disabled = true;
return;
}

Expand Down
4 changes: 2 additions & 2 deletions sample/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# yarn lockfile v1


"@moneytree/mt-link-javascript-sdk@file:../":
version "1.2.3"
"@moneytree/mt-link-javascript-sdk@file:..":
version "1.3.0"
dependencies:
conventional-changelog-cli "^2.0.28"
qs "^6.9.1"
Expand Down
18 changes: 15 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ interface IUrlConfig {
email?: string;
auth_action?: string;
show_auth_toggle?: boolean;
back_to?: string;
}

const commonUrlConfig = {
Expand Down Expand Up @@ -128,14 +129,25 @@ class LinkSDK {
}

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

const newCommonUrlConfig: ICommonUrlConfig & IUrlConfig = { ...commonUrlConfig };
const queryString = {
...this.oauthParams,
...this.params
};

if (backTo) {
delete queryString.redirect_uri;
newCommonUrlConfig.back_to = backTo;
}

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

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

0 comments on commit 9b9da89

Please sign in to comment.