[next] Support external config (#2088)

* fix: correctly determine expired token

* feat: support external config

* fix: lint

* fix: npm audit fix
This commit is contained in:
Kiwi
2018-11-27 18:54:55 +00:00
committed by Wyatt Johnson
parent 50cb46a053
commit 3fcb9a179a
15 changed files with 428 additions and 258 deletions
@@ -0,0 +1,20 @@
import { Child as PymChild } from "pym.js";
import { areWeInIframe } from "talk-framework/utils";
export interface ExternalConfig {
authToken?: string;
}
export function getExternalConfig(
pym?: PymChild
): Promise<ExternalConfig> | null {
if (pym && areWeInIframe()) {
return new Promise(resolve => {
pym.sendMessage("getConfig", "");
pym.onMessage("config", raw => {
resolve(JSON.parse(raw) as ExternalConfig);
});
});
}
return null;
}
+1 -1
View File
@@ -24,7 +24,7 @@ export function parseJWT(token: string, skewTolerance = 300): JWT {
header,
payload,
get expired() {
return Date.now() - skewTolerance < payload.exp;
return Date.now() / 1000 - skewTolerance >= payload.exp;
},
};
}
@@ -41,9 +41,12 @@ export function setAuthTokenInLocalState(
export async function initLocalBaseState(
environment: Environment,
{ localStorage }: TalkContext
{ localStorage }: TalkContext,
authToken?: string | null
) {
const authToken = await localStorage!.getItem("authToken");
if (authToken === undefined) {
authToken = await localStorage!.getItem("authToken");
}
commitLocalUpdate(environment, s => {
const root = s.getRoot();
@@ -53,7 +56,7 @@ export async function initLocalBaseState(
root.setLinkedRecord(localRecord, "local");
// Set auth token
setAuthTokenInLocalState(authToken, s);
setAuthTokenInLocalState(authToken || null, s);
// Create network Record
const networkRecord = createAndRetain(
@@ -0,0 +1,10 @@
/**
* Returns true if we are in an iframe.
*/
export default function areWeInIframe() {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
+1
View File
@@ -1,3 +1,4 @@
export { default as buildURL } from "./buildURL";
export { default as parseURL } from "./parseURL";
export { default as modifyQuery } from "./modifyQuery";
export { default as areWeInIframe } from "./areWeInIframe";