|
| 1 | +import LinkSDK from '@moneytree/mt-link-javascript-sdk'; |
| 2 | +import qs from 'qs'; |
| 3 | + |
| 4 | +const CLIENT_ID = 'client_id'; |
| 5 | +const TOKEN = 'access_token'; |
| 6 | +const AWESOME_APP_ID = 'af84f08f40970caf17f2e53b31771ceb50d0f32f7d44b826753982e809395290'; |
| 7 | + |
| 8 | +// Capture access token hash in URL |
| 9 | +window.onload = () => { |
| 10 | + let accessToken; |
| 11 | + |
| 12 | + const authorizeBtn = document.getElementById('authorize-btn'); |
| 13 | + const goToSettingsBtn = document.getElementById('settings-btn'); |
| 14 | + const goToVaultBtn = document.getElementById('vault-btn'); |
| 15 | + const tokenInfoLbl = document.getElementById('access-token-text'); |
| 16 | + |
| 17 | + const appInit = (clientId) => { |
| 18 | + LinkSDK.init({ |
| 19 | + clientId, |
| 20 | + response_type: 'token', |
| 21 | + scope: ['accounts_read', 'points_read'], |
| 22 | + redirectUri: 'https://localhost:9000', |
| 23 | + locale: 'ja-JP', |
| 24 | + isTestEnvironment: true |
| 25 | + }); |
| 26 | + |
| 27 | + // Launch authorize route when clicked |
| 28 | + authorizeBtn.onclick = () => { |
| 29 | + LinkSDK.authorize(); |
| 30 | + }; |
| 31 | + |
| 32 | + // Launch settings route when clicked |
| 33 | + goToSettingsBtn.onclick = () => { |
| 34 | + console.log('Settings'); |
| 35 | + LinkSDK.openSettings({ newTab: false }); |
| 36 | + }; |
| 37 | + |
| 38 | + // Launch vault route when clicked |
| 39 | + goToVaultBtn.onclick = () => { |
| 40 | + LinkSDK.openVault({ newTab: false }); |
| 41 | + }; |
| 42 | + }; |
| 43 | + |
| 44 | + console.log(Boolean(location.hash)); |
| 45 | + let clientId = AWESOME_APP_ID; |
| 46 | + |
| 47 | + const path = location.pathname; |
| 48 | + |
| 49 | + if (location.hash) { |
| 50 | + const hash = qs.parse(location.hash.slice(1)); |
| 51 | + accessToken = hash[TOKEN]; |
| 52 | + clientId = hash[CLIENT_ID]; |
| 53 | + document.getElementById('access-token-text').innerText = `Your access token is ${accessToken}.`; |
| 54 | + const authHeaders = new Headers({ |
| 55 | + method: 'GET', |
| 56 | + Authorization: `Bearer ${accessToken}` |
| 57 | + }); |
| 58 | + fetch('https://myaccount-staging.getmoneytree.com/oauth/token/info.json', { |
| 59 | + headers: authHeaders |
| 60 | + }) |
| 61 | + .then((response) => response.json()) |
| 62 | + .then((data) => { |
| 63 | + tokenInfoLbl.innerText = ` |
| 64 | + Your access token is ${accessToken}. |
| 65 | + It was generated for the app: ${data.aud.name}. |
| 66 | + It will expire on ${new Date(data.exp * 1000)}. |
| 67 | + It allows you to: ${data.scopes.join(', ')} |
| 68 | + `; |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + if (location.search) { |
| 73 | + const query = qs.parse(location.search.slice(1)); |
| 74 | + clientId = query[CLIENT_ID]; |
| 75 | + } |
| 76 | + |
| 77 | + if (!accessToken) { |
| 78 | + goToSettingsBtn.disabled = true; |
| 79 | + goToVaultBtn.disabled = true; |
| 80 | + } |
| 81 | + |
| 82 | + appInit(clientId); |
| 83 | +}; |
0 commit comments