mirror of
https://github.com/wassname/talk.git
synced 2026-08-10 12:41:02 +08:00
[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:
@@ -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;
|
||||
}
|
||||
@@ -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,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";
|
||||
|
||||
Reference in New Issue
Block a user