Skip to content

Commit

Permalink
feat: stash the accessToken in localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Nov 12, 2020
1 parent f76ad22 commit a8ce36c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
26 changes: 19 additions & 7 deletions packages/cosmic-swingset/lib/ag-solo/html/main.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
/* global WebSocket fetch document window walletFrame */
/* global WebSocket fetch document window walletFrame localStorage */
const RECONNECT_BACKOFF_SECONDS = 3;
// Functions to run to reset the HTML state to what it was.
const resetFns = [];
let inpBackground;

// Clear out the hash for privacy.
const accessTokenParams = `?${window.location.hash.slice(1)}`;
const accessTokenHash = window.location.hash;
// Fetch the access token from the window's URL.
let accessTokenParams = `?${window.location.hash.slice(1)}`;
let hasAccessToken = new URLSearchParams(accessTokenParams).get('accessToken');

try {
if (hasAccessToken) {
// Store the access token for later use.
localStorage.setItem('accessTokenParams', accessTokenParams);
} else {
// Try reviving it from localStorage.
accessTokenParams = localStorage.getItem('accessTokenParams') || '?';
hasAccessToken = new URLSearchParams(accessTokenParams).get('accessToken');
}
} catch (e) {
console.log('Error fetching accessTokenParams', e);
}

// Now that we've captured it, clear out the access token from the URL bar.
window.location.hash = '';
window.addEventListener('hashchange', _ev => {
// Keep it clear.
window.location.hash = '';
});
const hasAccessToken = new URLSearchParams(accessTokenParams).has(
'accessToken',
);

if (!hasAccessToken) {
// This is friendly advice to the user who doesn't know.
Expand Down
31 changes: 21 additions & 10 deletions packages/dapp-svelte-wallet/ui/src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@ import { makeWebSocket } from './websocket';
import { makeCapTPConnection } from './captp';

// Fetch the access token from the window's URL.
const accessTokenParams = `?${window.location.hash.slice(1)}`;
if (isProduction) {
// Now that we've captured it, clear out the access token from the URL bar.
window.location.hash = '';
window.addEventListener('hashchange', _ev => {
// Keep it clear.
window.location.hash = '';
});
}
const hasAccessToken = new URLSearchParams(accessTokenParams).has(
let accessTokenParams = `?${window.location.hash.slice(1)}`;
let hasAccessToken = new URLSearchParams(accessTokenParams).get(
'accessToken',
);
try {
if (hasAccessToken) {
// Store the access token for later use.
localStorage.setItem('accessTokenParams', accessTokenParams);
} else {
// Try reviving it from localStorage.
accessTokenParams = localStorage.getItem('accessTokenParams') || '?';
hasAccessToken = new URLSearchParams(accessTokenParams).get('accessToken');
}
} catch (e) {
console.log('Error fetching accessTokenParams', e);
}

// Now that we've captured it, clear out the access token from the URL bar.
window.location.hash = '';
window.addEventListener('hashchange', _ev => {
// Keep it clear.
window.location.hash = '';
});

if (!hasAccessToken) {
// This is friendly advice to the user who doesn't know.
Expand Down

0 comments on commit a8ce36c

Please sign in to comment.