Skip to content

Commit df6c626

Browse files
author
mt-roberto
committed
chore(sample): new logout + typescript implementation
1 parent cf586aa commit df6c626

File tree

7 files changed

+624
-1387
lines changed

7 files changed

+624
-1387
lines changed

sample/package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,23 @@
44
"author": "Moneytree",
55
"description": "An example of the Moneytree Web SDK.",
66
"license": "MIT",
7-
"main": "src/index.js",
7+
"main": "./src/index.ts",
88
"scripts": {
99
"start": "webpack-dev-server"
1010
},
1111
"devDependencies": {
12-
"@babel/core": "^7.5.4",
13-
"@babel/preset-env": "^7.5.4",
14-
"babel-loader": "^8.0.6",
15-
"clean-webpack-plugin": "^3.0.0",
1612
"css-loader": "^3.1.0",
1713
"html-webpack-plugin": "^3.2.0",
1814
"node-sass": "^4.12.0",
1915
"sass-loader": "^7.1.0",
2016
"style-loader": "^0.23.1",
17+
"ts-loader": "^6.2.1",
18+
"typescript": "^3.7.3",
2119
"webpack": "^4.35.3",
2220
"webpack-dev-server": "^3.7.2"
2321
},
2422
"dependencies": {
25-
"@moneytree/mt-link-javascript-sdk": "../",
23+
"@moneytree/mt-link-javascript-sdk": "file:../",
2624
"qs": "^6.7.0"
2725
}
2826
}

sample/src/index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
<div id="root">
99
<h3>Welcome to the Moneytree Link Web SDK Sample App</h3>
1010
<p id="access-token-text">You currently don't have an access token.</p>
11-
<input type="button" id="authorize-btn" class="btn" value="Authorize" />
12-
<input type="button" id="settings-btn" class="btn" value="Open Moneytree Settings" />
13-
<input type="button" id="vault-btn" class="btn" value="Open Vault" />
11+
<button id="authorize-btn">Authorize</button>
12+
<button id="settings-btn">Open Moneytree Settings</button>
13+
<button id="vault-btn">Open Vault</button>
14+
<button id="logout-btn">Logout</button>
1415
</div>
1516
</body>
1617
</html>

sample/src/index.js

Lines changed: 0 additions & 83 deletions
This file was deleted.

sample/src/index.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import LinkSDK from '@moneytree/mt-link-javascript-sdk';
2+
import qs from 'qs';
3+
4+
interface ITokenInfo {
5+
aud: {
6+
name: string;
7+
};
8+
exp: number;
9+
scopes: string[];
10+
}
11+
12+
const AWESOME_APP_ID = 'af84f08f40970caf17f2e53b31771ceb50d0f32f7d44b826753982e809395290';
13+
14+
const authorizeBtn = document.getElementById('authorize-btn') as HTMLButtonElement;
15+
const logoutBtn = document.getElementById('logout-btn') as HTMLButtonElement;
16+
const goToSettingsBtn = document.getElementById('settings-btn') as HTMLButtonElement;
17+
const goToVaultBtn = document.getElementById('vault-btn') as HTMLButtonElement;
18+
const tokenInfoLbl = document.getElementById('access-token-text') as HTMLButtonElement;
19+
const accessTokenLabel = document.getElementById('access-token-text') as HTMLParagraphElement;
20+
21+
if (!authorizeBtn || !logoutBtn || !goToSettingsBtn || !goToVaultBtn) {
22+
throw new Error('An error occurred');
23+
}
24+
25+
// Launch authorize route when clicked
26+
authorizeBtn.onclick = () => {
27+
LinkSDK.authorize();
28+
};
29+
30+
// Launch logout route when clicked
31+
logoutBtn.onclick = () => {
32+
LinkSDK.logout();
33+
};
34+
35+
// Launch settings route when clicked
36+
goToSettingsBtn.onclick = () => {
37+
LinkSDK.openSettings({ newTab: false });
38+
};
39+
40+
// Launch vault route when clicked
41+
goToVaultBtn.onclick = () => {
42+
LinkSDK.openVault({ newTab: false });
43+
};
44+
45+
const initializeLinkSDK = () => {
46+
LinkSDK.init({
47+
clientId: AWESOME_APP_ID,
48+
responseType: 'token',
49+
scope: ['accounts_read', 'points_read'],
50+
redirectUri: 'https://localhost:9000',
51+
locale: 'ja-JP',
52+
isTestEnvironment: true
53+
});
54+
};
55+
56+
const validateToken = async () => {
57+
const { hash, search } = location;
58+
const accessToken =
59+
qs.parse(hash.slice(1)).access_token || qs.parse(search, { ignoreQueryPrefix: true }).access_token;
60+
61+
// Disables buttons when a session has not been initialized.
62+
if (!accessToken) {
63+
goToSettingsBtn.disabled = true;
64+
goToVaultBtn.disabled = true;
65+
logoutBtn.disabled = true;
66+
return;
67+
}
68+
69+
accessTokenLabel.innerText = `Your access token is ${accessToken}.`;
70+
71+
const authHeaders = new Headers({
72+
method: 'GET',
73+
Authorization: `Bearer ${accessToken}`
74+
});
75+
76+
const response = await fetch('https://myaccount-staging.getmoneytree.com/oauth/token/info.json', {
77+
headers: authHeaders
78+
});
79+
80+
const data: ITokenInfo = await response.json();
81+
82+
tokenInfoLbl.innerText = `
83+
Your access token is ${accessToken}.
84+
It was generated for the app: ${data.aud.name}.
85+
It will expire on ${new Date(data.exp * 1000)}.
86+
It allows you to: ${data.scopes.join(', ')}
87+
`;
88+
};
89+
90+
initializeLinkSDK();
91+
// tslint:disable-next-line: no-floating-promises
92+
validateToken();

sample/tsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"module": "commonjs",
5+
"target": "es5",
6+
"lib": ["es6", "es2017", "dom"]
7+
}
8+
}

sample/webpack.config.js

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
const path = require('path');
22
const webpack = require('webpack');
33
const HtmlWebpackPlugin = require('html-webpack-plugin');
4-
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
54

65
module.exports = {
76
mode: 'development',
87

9-
context: path.join(process.cwd(), 'src'),
10-
entry: {
11-
app: './index.js'
12-
},
13-
14-
output: {
15-
path: path.join(process.cwd(), 'dist'),
16-
filename: '[name].[hash].js',
17-
publicPath: '/',
18-
sourceMapFilename: '[name].map'
8+
resolve: {
9+
extensions: ['.ts', '.js']
1910
},
2011

2112
devtool: 'source-map', // enhance debugging by adding meta info for the browser devtools
@@ -24,7 +15,6 @@ module.exports = {
2415
publicPath: '/',
2516
port: 9000,
2617
https: true,
27-
contentBase: path.join(process.cwd(), 'dist'), // static file location
2818
host: 'localhost',
2919
historyApiFallback: true, // true for index.html upon 404, object for multiple paths
3020
noInfo: false,
@@ -35,20 +25,14 @@ module.exports = {
3525
module: {
3626
rules: [
3727
{
38-
test: /\.js$/,
39-
use: {
40-
loader: 'babel-loader',
41-
options: {
42-
presets: ['@babel/preset-env']
43-
}
44-
}
28+
test: /\.tsx?$/,
29+
loader: 'ts-loader'
4530
}
4631
]
4732
},
4833
plugins: [
49-
new CleanWebpackPlugin(),
5034
new HtmlWebpackPlugin({
51-
template: 'index.html'
35+
template: './src/index.html'
5236
}),
5337
new webpack.HotModuleReplacementPlugin()
5438
]

0 commit comments

Comments
 (0)