Skip to content

Commit a8ce36c

Browse files
committed
feat: stash the accessToken in localStorage
1 parent f76ad22 commit a8ce36c

File tree

2 files changed

+40
-17
lines changed
  • packages

2 files changed

+40
-17
lines changed

packages/cosmic-swingset/lib/ag-solo/html/main.js

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
1-
/* global WebSocket fetch document window walletFrame */
1+
/* global WebSocket fetch document window walletFrame localStorage */
22
const RECONNECT_BACKOFF_SECONDS = 3;
33
// Functions to run to reset the HTML state to what it was.
44
const resetFns = [];
55
let inpBackground;
66

7-
// Clear out the hash for privacy.
8-
const accessTokenParams = `?${window.location.hash.slice(1)}`;
9-
const accessTokenHash = window.location.hash;
7+
// Fetch the access token from the window's URL.
8+
let accessTokenParams = `?${window.location.hash.slice(1)}`;
9+
let hasAccessToken = new URLSearchParams(accessTokenParams).get('accessToken');
10+
11+
try {
12+
if (hasAccessToken) {
13+
// Store the access token for later use.
14+
localStorage.setItem('accessTokenParams', accessTokenParams);
15+
} else {
16+
// Try reviving it from localStorage.
17+
accessTokenParams = localStorage.getItem('accessTokenParams') || '?';
18+
hasAccessToken = new URLSearchParams(accessTokenParams).get('accessToken');
19+
}
20+
} catch (e) {
21+
console.log('Error fetching accessTokenParams', e);
22+
}
23+
24+
// Now that we've captured it, clear out the access token from the URL bar.
1025
window.location.hash = '';
1126
window.addEventListener('hashchange', _ev => {
1227
// Keep it clear.
1328
window.location.hash = '';
1429
});
15-
const hasAccessToken = new URLSearchParams(accessTokenParams).has(
16-
'accessToken',
17-
);
1830

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

packages/dapp-svelte-wallet/ui/src/store.js

+21-10
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,29 @@ import { makeWebSocket } from './websocket';
66
import { makeCapTPConnection } from './captp';
77

88
// Fetch the access token from the window's URL.
9-
const accessTokenParams = `?${window.location.hash.slice(1)}`;
10-
if (isProduction) {
11-
// Now that we've captured it, clear out the access token from the URL bar.
12-
window.location.hash = '';
13-
window.addEventListener('hashchange', _ev => {
14-
// Keep it clear.
15-
window.location.hash = '';
16-
});
17-
}
18-
const hasAccessToken = new URLSearchParams(accessTokenParams).has(
9+
let accessTokenParams = `?${window.location.hash.slice(1)}`;
10+
let hasAccessToken = new URLSearchParams(accessTokenParams).get(
1911
'accessToken',
2012
);
13+
try {
14+
if (hasAccessToken) {
15+
// Store the access token for later use.
16+
localStorage.setItem('accessTokenParams', accessTokenParams);
17+
} else {
18+
// Try reviving it from localStorage.
19+
accessTokenParams = localStorage.getItem('accessTokenParams') || '?';
20+
hasAccessToken = new URLSearchParams(accessTokenParams).get('accessToken');
21+
}
22+
} catch (e) {
23+
console.log('Error fetching accessTokenParams', e);
24+
}
25+
26+
// Now that we've captured it, clear out the access token from the URL bar.
27+
window.location.hash = '';
28+
window.addEventListener('hashchange', _ev => {
29+
// Keep it clear.
30+
window.location.hash = '';
31+
});
2132

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

0 commit comments

Comments
 (0)