feat: fetch access token from indexedDb

develop
c-cal 4 years ago
parent fa8ae33d15
commit ba6ef75747
  1. 37
      src/App.js
  2. 91
      src/StorageManager.js

@ -1,11 +1,13 @@
import React, { Suspense, useEffect, useState } from "react";
import { RestfulProvider } from "restful-react";
import sdk from "matrix-js-sdk";
import { MatrixClientContext } from "./contexts";
import * as StorageManager from "./StorageManager";
import AdminHome from "./AdminHome";
import DelayedSpinner from "./DelayedSpinner";
import Login from "./Login";
import { MatrixClientContext } from "./contexts";
import "./css/App.scss";
@ -17,21 +19,24 @@ export default () => {
useEffect(() => {
Promise.resolve(getHsBaseUrl()).then(baseUrl => {
let client;
const accessToken = localStorage.getItem("mx_access_token");
const userId = localStorage.getItem("mx_user_id");
if (accessToken && userId) {
client = sdk.createClient({
baseUrl,
accessToken,
userId,
});
setupClient(client);
} else {
client = sdk.createClient({ baseUrl });
setMissingAccessToken(true);
}
setClient(client);
StorageManager.idbLoad("account", "mx_access_token").then(accessToken => {
if (!accessToken) {
accessToken = localStorage.getItem("mx_access_token");
}
const userId = localStorage.getItem("mx_user_id");
if (accessToken && userId) {
client = sdk.createClient({
baseUrl,
accessToken,
userId,
});
setupClient(client);
} else {
client = sdk.createClient({ baseUrl });
setMissingAccessToken(true);
}
setClient(client);
});
});
}, []);

@ -0,0 +1,91 @@
/*
Copyright 2019 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// just *accessing* indexedDB throws an exception in firefox if disabled.
let indexedDB;
try {
indexedDB = window.indexedDB;
} catch (e) {}
let idb = null;
async function idbInit() {
if (!indexedDB) {
throw new Error("IndexedDB not available");
}
idb = await new Promise((resolve, reject) => {
const request = indexedDB.open("matrix-react-sdk", 1);
request.onerror = reject;
request.onsuccess = event => {
resolve(request.result);
};
request.onupgradeneeded = event => {
const db = request.result;
db.createObjectStore("account");
};
});
}
export async function idbLoad(table, key) {
if (!idb) {
await idbInit();
}
return new Promise((resolve, reject) => {
const txn = idb.transaction([table], "readonly");
txn.onerror = reject;
const objectStore = txn.objectStore(table);
const request = objectStore.get(key);
request.onerror = reject;
request.onsuccess = event => {
resolve(request.result);
};
});
}
export async function idbSave(table: string, key: string | string[], data: any): Promise<void> {
if (!idb) {
await idbInit();
}
return new Promise((resolve, reject) => {
const txn = idb.transaction([table], "readwrite");
txn.onerror = reject;
const objectStore = txn.objectStore(table);
const request = objectStore.put(data, key);
request.onerror = reject;
request.onsuccess = event => {
resolve();
};
});
}
export async function idbDelete(table: string, key: string | string[]): Promise<void> {
if (!idb) {
await idbInit();
}
return new Promise((resolve, reject) => {
const txn = idb.transaction([table], "readwrite");
txn.onerror = reject;
const objectStore = txn.objectStore(table);
const request = objectStore.delete(key);
request.onerror = reject;
request.onsuccess = event => {
resolve();
};
});
}
Loading…
Cancel
Save