mirror of
https://github.com/matrix-org/matrix-hookshot.git
synced 2025-03-10 21:19:13 +00:00
Continue even if localstorage is inaccessible (#678)
* Ignore storage failures * changelog
This commit is contained in:
parent
61f25fae36
commit
b5b86d45da
1
changelog.d/678.bugfix
Normal file
1
changelog.d/678.bugfix
Normal file
@ -0,0 +1 @@
|
|||||||
|
Ensure the widget still works without needing to store local storage data.
|
@ -23,20 +23,27 @@ interface RequestOpts {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class BridgeAPI {
|
export class BridgeAPI {
|
||||||
|
|
||||||
static async getBridgeAPI(baseUrl: string, widgetApi: WidgetApi): Promise<BridgeAPI> {
|
static async getBridgeAPI(baseUrl: string, widgetApi: WidgetApi): Promise<BridgeAPI> {
|
||||||
const sessionToken = localStorage.getItem('hookshot-sessionToken');
|
try {
|
||||||
baseUrl = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
|
const sessionToken = localStorage.getItem('hookshot-sessionToken');
|
||||||
if (sessionToken) {
|
baseUrl = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
|
||||||
const client = new BridgeAPI(baseUrl, sessionToken);
|
if (sessionToken) {
|
||||||
try {
|
const client = new BridgeAPI(baseUrl, sessionToken);
|
||||||
await client.verify();
|
try {
|
||||||
return client;
|
await client.verify();
|
||||||
} catch (ex) {
|
return client;
|
||||||
// TODO: Check that the token is actually invalid, rather than just assuming we need to refresh.
|
} catch (ex) {
|
||||||
console.warn(`Failed to verify token, fetching new token`, ex);
|
// TODO: Check that the token is actually invalid, rather than just assuming we need to refresh.
|
||||||
localStorage.removeItem(sessionToken);
|
console.warn(`Failed to verify token, fetching new token`, ex);
|
||||||
|
localStorage.removeItem(sessionToken);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch (ex) {
|
||||||
|
// E.g. Browser prevents storage access.
|
||||||
|
console.debug(`Failed to fetch session token, requesting new token`, ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
const creds = await widgetApi.requestOpenIDConnectToken();
|
const creds = await widgetApi.requestOpenIDConnectToken();
|
||||||
const { matrix_server_name, access_token } = creds;
|
const { matrix_server_name, access_token } = creds;
|
||||||
// eslint-disable-next-line camelcase
|
// eslint-disable-next-line camelcase
|
||||||
@ -66,7 +73,12 @@ export class BridgeAPI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const response = await res.json() as ExchangeOpenAPIResponseBody;
|
const response = await res.json() as ExchangeOpenAPIResponseBody;
|
||||||
localStorage.setItem('hookshot-sessionToken', response.token);
|
try {
|
||||||
|
localStorage.setItem('hookshot-sessionToken', response.token);
|
||||||
|
} catch (ex) {
|
||||||
|
// E.g. Browser prevents storage access.
|
||||||
|
console.debug(`Failed to store session token, continuing`, ex);
|
||||||
|
}
|
||||||
return new BridgeAPI(baseUrl, response.token);
|
return new BridgeAPI(baseUrl, response.token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user