mirror of
https://github.com/wassname/talk.git
synced 2026-08-13 12:40:11 +08:00
chore: harmonize authToken to accessToken (#2184)
This commit is contained in:
@@ -93,7 +93,7 @@ function createRelayEnvironment() {
|
||||
const tokenGetter: TokenGetter = () => {
|
||||
const localState = source.get(LOCAL_ID);
|
||||
if (localState) {
|
||||
return (localState.loggedIn && localState.authToken) || "";
|
||||
return (localState.loggedIn && localState.accessToken) || "";
|
||||
}
|
||||
return "";
|
||||
};
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Child as PymChild } from "pym.js";
|
||||
import { areWeInIframe } from "talk-framework/utils";
|
||||
|
||||
export interface ExternalConfig {
|
||||
authToken?: string;
|
||||
accessToken?: string;
|
||||
}
|
||||
|
||||
export function getExternalConfig(
|
||||
|
||||
@@ -17,5 +17,5 @@ export { graphql } from "react-relay";
|
||||
export {
|
||||
default as commitLocalUpdatePromisified,
|
||||
} from "./commitLocalUpdatePromisified";
|
||||
export { initLocalBaseState, setAuthTokenInLocalState } from "./localState";
|
||||
export { initLocalBaseState, setAccessTokenInLocalState } from "./localState";
|
||||
export { default as fetchQuery } from "./fetchQuery";
|
||||
|
||||
@@ -21,20 +21,20 @@ export const LOCAL_ID = "client:root.local";
|
||||
export const NETWORK_TYPE = "Network";
|
||||
export const NETWORK_ID = "client:root.local.network";
|
||||
|
||||
export function setAuthTokenInLocalState(
|
||||
authToken: string | null,
|
||||
export function setAccessTokenInLocalState(
|
||||
accessToken: string | null,
|
||||
source: RecordSourceProxy
|
||||
) {
|
||||
const localRecord = source.get(LOCAL_ID)!;
|
||||
localRecord.setValue(authToken || "", "authToken");
|
||||
if (authToken) {
|
||||
const { payload, expired } = parseJWT(authToken);
|
||||
localRecord.setValue(payload.exp, "authExp");
|
||||
localRecord.setValue(payload.jti, "authJTI");
|
||||
localRecord.setValue(accessToken || "", "accessToken");
|
||||
if (accessToken) {
|
||||
const { payload, expired } = parseJWT(accessToken);
|
||||
localRecord.setValue(payload.exp, "accessTokenExp");
|
||||
localRecord.setValue(payload.jti, "accessTokenJTI");
|
||||
localRecord.setValue(!expired, "loggedIn");
|
||||
} else {
|
||||
localRecord.setValue(null, "authExp");
|
||||
localRecord.setValue(null, "authJTI");
|
||||
localRecord.setValue(null, "accessTokenExp");
|
||||
localRecord.setValue(null, "accessTokenJTI");
|
||||
localRecord.setValue(false, "loggedIn");
|
||||
}
|
||||
}
|
||||
@@ -42,10 +42,10 @@ export function setAuthTokenInLocalState(
|
||||
export async function initLocalBaseState(
|
||||
environment: Environment,
|
||||
{ localStorage }: TalkContext,
|
||||
authToken?: string | null
|
||||
accessToken?: string | null
|
||||
) {
|
||||
if (authToken === undefined) {
|
||||
authToken = await localStorage!.getItem("authToken");
|
||||
if (accessToken === undefined) {
|
||||
accessToken = await localStorage!.getItem("accessToken");
|
||||
}
|
||||
|
||||
commitLocalUpdate(environment, s => {
|
||||
@@ -56,7 +56,7 @@ export async function initLocalBaseState(
|
||||
root.setLinkedRecord(localRecord, "local");
|
||||
|
||||
// Set auth token
|
||||
setAuthTokenInLocalState(authToken || null, s);
|
||||
setAccessTokenInLocalState(accessToken || null, s);
|
||||
|
||||
// Create network Record
|
||||
const networkRecord = createAndRetain(
|
||||
|
||||
+12
-10
@@ -5,11 +5,11 @@ import { TalkContext } from "talk-framework/lib/bootstrap";
|
||||
import { LOCAL_ID } from "talk-framework/lib/relay";
|
||||
import { createPromisifiedStorage } from "talk-framework/lib/storage";
|
||||
import {
|
||||
createAuthToken,
|
||||
createAccessToken,
|
||||
createRelayEnvironment,
|
||||
} from "talk-framework/testHelpers";
|
||||
|
||||
import { commit } from "./SetAuthTokenMutation";
|
||||
import { commit } from "./SetAccessTokenMutation";
|
||||
|
||||
let environment: Environment;
|
||||
const source: RecordSource = new RecordSource();
|
||||
@@ -20,7 +20,7 @@ beforeAll(() => {
|
||||
});
|
||||
});
|
||||
|
||||
const authToken = createAuthToken();
|
||||
const accessToken = createAccessToken();
|
||||
|
||||
it("Sets auth token to localStorage", async () => {
|
||||
const clearSessionStub = sinon.stub();
|
||||
@@ -28,10 +28,10 @@ it("Sets auth token to localStorage", async () => {
|
||||
localStorage: createPromisifiedStorage(),
|
||||
clearSession: clearSessionStub,
|
||||
};
|
||||
await commit(environment, { authToken }, context as any);
|
||||
expect(source.get(LOCAL_ID)!.authToken).toEqual(authToken);
|
||||
await expect(context.localStorage!.getItem("authToken")).resolves.toEqual(
|
||||
authToken
|
||||
await commit(environment, { accessToken }, context as any);
|
||||
expect(source.get(LOCAL_ID)!.accessToken).toEqual(accessToken);
|
||||
await expect(context.localStorage!.getItem("accessToken")).resolves.toEqual(
|
||||
accessToken
|
||||
);
|
||||
expect(clearSessionStub.calledOnce).toBe(true);
|
||||
});
|
||||
@@ -42,8 +42,10 @@ it("Removes auth token from localStorage", async () => {
|
||||
localStorage: createPromisifiedStorage(),
|
||||
clearSession: clearSessionStub,
|
||||
};
|
||||
localStorage.setItem("authToken", authToken);
|
||||
await commit(environment, { authToken: null }, context as any);
|
||||
await expect(context.localStorage!.getItem("authToken")).resolves.toBeNull();
|
||||
localStorage.setItem("accessToken", accessToken);
|
||||
await commit(environment, { accessToken: null }, context as any);
|
||||
await expect(
|
||||
context.localStorage!.getItem("accessToken")
|
||||
).resolves.toBeNull();
|
||||
expect(clearSessionStub.calledOnce).toBe(true);
|
||||
});
|
||||
+13
-11
@@ -4,33 +4,35 @@ import { TalkContext } from "talk-framework/lib/bootstrap";
|
||||
import {
|
||||
commitLocalUpdatePromisified,
|
||||
createMutationContainer,
|
||||
setAuthTokenInLocalState,
|
||||
setAccessTokenInLocalState,
|
||||
} from "talk-framework/lib/relay";
|
||||
|
||||
export interface SetAuthTokenInput {
|
||||
authToken: string | null;
|
||||
export interface SetAccessTokenInput {
|
||||
accessToken: string | null;
|
||||
}
|
||||
|
||||
export type SetAuthTokenMutation = (input: SetAuthTokenInput) => Promise<void>;
|
||||
export type SetAccessTokenMutation = (
|
||||
input: SetAccessTokenInput
|
||||
) => Promise<void>;
|
||||
|
||||
export async function commit(
|
||||
environment: Environment,
|
||||
input: SetAuthTokenInput,
|
||||
input: SetAccessTokenInput,
|
||||
{ localStorage, clearSession }: TalkContext
|
||||
) {
|
||||
await commitLocalUpdatePromisified(environment, async store => {
|
||||
setAuthTokenInLocalState(input.authToken, store);
|
||||
if (input.authToken) {
|
||||
await localStorage.setItem("authToken", input.authToken);
|
||||
setAccessTokenInLocalState(input.accessToken, store);
|
||||
if (input.accessToken) {
|
||||
await localStorage.setItem("accessToken", input.accessToken);
|
||||
} else {
|
||||
await localStorage.removeItem("authToken");
|
||||
await localStorage.removeItem("accessToken");
|
||||
}
|
||||
// Clear current session, as we are starting a new one.
|
||||
await clearSession();
|
||||
});
|
||||
}
|
||||
|
||||
export const withSetAuthTokenMutation = createMutationContainer(
|
||||
"setAuthToken",
|
||||
export const withSetAccessTokenMutation = createMutationContainer(
|
||||
"setAccessToken",
|
||||
commit
|
||||
);
|
||||
@@ -2,7 +2,7 @@ import { Environment } from "relay-runtime";
|
||||
import { TalkContext } from "talk-framework/lib/bootstrap";
|
||||
import { createMutationContainer } from "talk-framework/lib/relay";
|
||||
import signOut from "../rest/signOut";
|
||||
import { commit as setAuthToken } from "./SetAuthTokenMutation";
|
||||
import { commit as setAccessToken } from "./SetAccessTokenMutation";
|
||||
|
||||
export type SignOutMutation = () => Promise<void>;
|
||||
|
||||
@@ -12,7 +12,7 @@ export async function commit(
|
||||
ctx: TalkContext
|
||||
) {
|
||||
await signOut(ctx.rest);
|
||||
await setAuthToken(environment, { authToken: "" }, ctx);
|
||||
await setAccessToken(environment, { accessToken: "" }, ctx);
|
||||
}
|
||||
|
||||
export const withSignOutMutation = createMutationContainer("signOut", commit);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
export {
|
||||
withSetAuthTokenMutation,
|
||||
SetAuthTokenMutation,
|
||||
SetAuthTokenInput,
|
||||
} from "./SetAuthTokenMutation";
|
||||
withSetAccessTokenMutation,
|
||||
SetAccessTokenMutation,
|
||||
SetAccessTokenInput,
|
||||
} from "./SetAccessTokenMutation";
|
||||
export { withSignOutMutation, SignOutMutation } from "./SignOutMutation";
|
||||
export {
|
||||
withSetNetworkStatusMutation,
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
export default function createAuthToken() {
|
||||
export default function createAccessToken() {
|
||||
return `${btoa(
|
||||
JSON.stringify({
|
||||
alg: "HS256",
|
||||
@@ -19,6 +19,6 @@ export { default as waitUntilThrow } from "./waitUntilThrow";
|
||||
export { default as matchText } from "./matchText";
|
||||
export { default as toJSON } from "./toJSON";
|
||||
export { default as replaceHistoryLocation } from "./replaceHistoryLocation";
|
||||
export { default as createAuthToken } from "./createAuthToken";
|
||||
export { default as createAccessToken } from "./createAccessToken";
|
||||
export { default as findParentsWithType } from "./findParentsWithType";
|
||||
export { default as findParentWithType } from "./findParentWithType";
|
||||
|
||||
Reference in New Issue
Block a user