chore: harmonize authToken to accessToken (#2184)

This commit is contained in:
Kiwi
2019-02-08 23:40:20 +01:00
committed by GitHub
parent 7ec6780ee1
commit 710de07fa6
47 changed files with 172 additions and 160 deletions
+3 -3
View File
@@ -12,9 +12,9 @@ enum View {
type Local {
network: Network!
authToken: String
authExp: Int
authJTI: String
accessToken: String
accessTokenExp: Int
accessTokenJTI: String
loggedIn: Boolean!
redirectPath: String
authView: View
@@ -2,10 +2,10 @@ import { Environment } from "relay-runtime";
import { TalkContext } from "talk-framework/lib/bootstrap";
import { createMutationContainer } from "talk-framework/lib/relay";
import { commit as setAuthToken } from "talk-framework/mutations/SetAuthTokenMutation";
import { commit as setAccessToken } from "talk-framework/mutations/SetAccessTokenMutation";
interface CompleteAccountInput {
authToken: string;
accessToken: string;
}
export type CompleteAccountMutation = (
input: CompleteAccountInput
@@ -16,7 +16,11 @@ export async function commit(
input: CompleteAccountInput,
context: TalkContext
) {
await setAuthToken(environment, { authToken: input.authToken }, context);
await setAccessToken(
environment,
{ accessToken: input.accessToken },
context
);
}
export const withCompleteAccountMutation = createMutationContainer(
@@ -54,9 +54,11 @@ function handleAccountCompletion(props: Props) {
setAuthView({ view: "CREATE_PASSWORD" });
}
} else {
props.completeAccount({ authToken: props.local.authToken! }).then(() => {
props.router.replace(props.local.redirectPath || "/admin");
});
props
.completeAccount({ accessToken: props.local.accessToken! })
.then(() => {
props.router.replace(props.local.redirectPath || "/admin");
});
return true;
}
}
@@ -100,7 +102,7 @@ class AccountCompletionContainer extends Component<Props, State> {
const enhanced = withLocalStateContainer(
graphql`
fragment AccountCompletionContainerLocal on Local {
authToken
accessToken
authView
redirectPath
}
@@ -2,7 +2,7 @@ import { get, merge } from "lodash";
import sinon from "sinon";
import {
createAuthToken,
createAccessToken,
replaceHistoryLocation,
wait,
waitForElement,
@@ -44,7 +44,7 @@ async function createTestRenderer(
initLocalState: localRecord => {
localRecord.setValue("SIGN_IN", "authView");
localRecord.setValue(true, "loggedIn");
localRecord.setValue(createAuthToken(), "authToken");
localRecord.setValue(createAccessToken(), "accessToken");
},
});
@@ -2,7 +2,7 @@ import { get, merge } from "lodash";
import sinon from "sinon";
import {
createAuthToken,
createAccessToken,
replaceHistoryLocation,
toJSON,
wait,
@@ -37,7 +37,7 @@ async function createTestRenderer(
initLocalState: localRecord => {
localRecord.setValue("ADD_EMAIL_ADDRESS", "authView");
localRecord.setValue(true, "loggedIn");
localRecord.setValue(createAuthToken(), "authToken");
localRecord.setValue(createAccessToken(), "accessToken");
},
});
const container = await waitForElement(() =>
@@ -2,7 +2,7 @@ import { get, merge } from "lodash";
import sinon from "sinon";
import {
createAuthToken,
createAccessToken,
replaceHistoryLocation,
toJSON,
wait,
@@ -37,7 +37,7 @@ async function createTestRenderer(
initLocalState: localRecord => {
localRecord.setValue("CREATE_PASSWORD", "authView");
localRecord.setValue(true, "loggedIn");
localRecord.setValue(createAuthToken(), "authToken");
localRecord.setValue(createAccessToken(), "accessToken");
},
});
const container = await waitForElement(() =>
@@ -2,7 +2,7 @@ import { get, merge } from "lodash";
import sinon from "sinon";
import {
createAuthToken,
createAccessToken,
replaceHistoryLocation,
toJSON,
wait,
@@ -36,7 +36,7 @@ async function createTestRenderer(
initLocalState: localRecord => {
localRecord.setValue("CREATE_USERNAME", "authView");
localRecord.setValue(true, "loggedIn");
localRecord.setValue(createAuthToken(), "authToken");
localRecord.setValue(createAccessToken(), "accessToken");
},
});
const container = await waitForElement(() =>
@@ -1,6 +1,6 @@
import sinon from "sinon";
import {
createAuthToken,
createAccessToken,
replaceHistoryLocation,
wait,
} from "talk-framework/testHelpers";
@@ -30,7 +30,7 @@ it("redirect when already logged in", async () => {
logNetwork: false,
initLocalState: localRecord => {
localRecord.setValue(true, "loggedIn");
localRecord.setValue(createAuthToken(), "authToken");
localRecord.setValue(createAccessToken(), "accessToken");
},
});
await wait(() =>
@@ -48,7 +48,7 @@ it("redirect to redirectPath when already logged in", async () => {
logNetwork: false,
initLocalState: localRecord => {
localRecord.setValue(true, "loggedIn");
localRecord.setValue(createAuthToken(), "authToken");
localRecord.setValue(createAccessToken(), "accessToken");
localRecord.setValue("/admin/community", "redirectPath");
},
});
@@ -4,7 +4,7 @@ import sinon from "sinon";
import { TalkContext } from "talk-framework/lib/bootstrap";
import { LOCAL_ID } from "talk-framework/lib/relay";
import {
createAuthToken,
createAccessToken,
replaceHistoryLocation,
wait,
waitForElement,
@@ -40,7 +40,7 @@ function createTestRenderer(
logNetwork: false,
initLocalState: localRecord => {
localRecord.setValue(true, "loggedIn");
localRecord.setValue(createAuthToken(), "authToken");
localRecord.setValue(createAccessToken(), "accessToken");
localRecord.setValue("SIGN_IN", "authView");
},
});
@@ -2,7 +2,7 @@ import { ReactTestInstance } from "react-test-renderer";
import sinon from "sinon";
import {
createAuthToken,
createAccessToken,
replaceHistoryLocation,
wait,
waitForElement,
@@ -128,7 +128,7 @@ it("submits form successfully", async () => {
.find(inputPredicate("password"))
.props.onChange({ target: { value: "testtest" } });
const authToken = createAuthToken();
const accessToken = createAccessToken();
const restMock = sinon.mock(context.rest);
restMock
@@ -141,7 +141,7 @@ it("submits form successfully", async () => {
},
})
.once()
.returns({ token: authToken });
.returns({ token: accessToken });
const historyMock = sinon.mock(window.history);
@@ -26,7 +26,7 @@ interface Props {
function handleAccountCompletion(props: Props) {
const {
local: { view, authToken },
local: { view, accessToken },
me,
auth,
setView,
@@ -55,7 +55,7 @@ function handleAccountCompletion(props: Props) {
}
return false;
}
completeAccount({ authToken: authToken! });
completeAccount({ accessToken: accessToken! });
return true;
}
return false;
@@ -91,7 +91,7 @@ class AccountCompletionContainer extends Component<Props, State> {
const enhanced = withLocalStateContainer(
graphql`
fragment AccountCompletionContainerLocal on Local {
authToken
accessToken
view
}
`
@@ -4,8 +4,8 @@ exports[`get auth token from url 1`] = `
"{
\\"__id\\": \\"client:root.local\\",
\\"__typename\\": \\"Local\\",
\\"authToken\\": \\"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIzMWIyNjU5MS00ZTlhLTQzODgtYTdmZi1lMWJkYzVkOTdjY2UifQ==\\",
\\"authJTI\\": \\"31b26591-4e9a-4388-a7ff-e1bdc5d97cce\\",
\\"accessToken\\": \\"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIzMWIyNjU5MS00ZTlhLTQzODgtYTdmZi1lMWJkYzVkOTdjY2UifQ==\\",
\\"accessTokenJTI\\": \\"31b26591-4e9a-4388-a7ff-e1bdc5d97cce\\",
\\"loggedIn\\": true,
\\"network\\": {
\\"__ref\\": \\"client:root.local.network\\"
@@ -27,9 +27,9 @@ exports[`init local state 1`] = `
\\"client:root.local\\": {
\\"__id\\": \\"client:root.local\\",
\\"__typename\\": \\"Local\\",
\\"authToken\\": \\"\\",
\\"authExp\\": null,
\\"authJTI\\": null,
\\"accessToken\\": \\"\\",
\\"accessTokenExp\\": null,
\\"accessTokenJTI\\": null,
\\"loggedIn\\": false,
\\"network\\": {
\\"__ref\\": \\"client:root.local.network\\"
@@ -2,7 +2,7 @@ import { Environment, RecordSource } from "relay-runtime";
import { LOCAL_ID } from "talk-framework/lib/relay";
import {
createAuthToken,
createAccessToken,
createRelayEnvironment,
replaceHistoryLocation,
} from "talk-framework/testHelpers";
@@ -41,7 +41,7 @@ it("set view from query", async () => {
it("get auth token from url", async () => {
const restoreHistoryLocation = replaceHistoryLocation(
`http://localhost/#accessToken=${createAuthToken()}`
`http://localhost/#accessToken=${createAccessToken()}`
);
await initLocalState(environment, context as any);
expect(JSON.stringify(source.get(LOCAL_ID), null, 2)).toMatchSnapshot();
+3 -3
View File
@@ -9,9 +9,9 @@ enum View {
}
type Local {
authToken: String
authExp: Int
authJTI: String
accessToken: String
accessTokenExp: Int
accessTokenJTI: String
loggedIn: Boolean!
view: View!
error: String
@@ -4,7 +4,7 @@ import { TalkContext } from "talk-framework/lib/bootstrap";
import { createMutationContainer } from "talk-framework/lib/relay";
export interface CompleteAccountInput {
authToken: string;
accessToken: string;
}
export type CompleteAccountMutation = (
input: CompleteAccountInput
@@ -15,7 +15,7 @@ export async function commit(
input: CompleteAccountInput,
{ postMessage }: TalkContext
) {
postMessage.send("setAuthToken", input.authToken, window.opener);
postMessage.send("setAccessToken", input.accessToken, window.opener);
window.close();
}
@@ -2,7 +2,7 @@ import { get, merge } from "lodash";
import sinon from "sinon";
import {
createAuthToken,
createAccessToken,
wait,
waitForElement,
within,
@@ -14,7 +14,7 @@ import mockWindow from "./mockWindow";
let windowMock: ReturnType<typeof mockWindow>;
const authToken = createAuthToken();
const accessToken = createAccessToken();
async function createTestRenderer(
customResolver: any = {},
@@ -42,7 +42,7 @@ async function createTestRenderer(
resolvers,
initLocalState: localRecord => {
localRecord.setValue("CREATE_PASSWORD", "view");
localRecord.setValue(authToken, "authToken");
localRecord.setValue(accessToken, "accessToken");
},
});
@@ -132,7 +132,7 @@ it("send back auth token", async () => {
const postMessageMock = sinon.mock(context.postMessage);
postMessageMock
.expects("send")
.withArgs("setAuthToken", authToken, window.opener)
.withArgs("setAccessToken", accessToken, window.opener)
.atLeast(1);
// Wait till window is closed.
+3 -3
View File
@@ -164,7 +164,7 @@ it("shows server error", async () => {
it("submits form successfully", async () => {
const { form, context } = await createTestRenderer();
const { getByLabelText } = within(form!);
const authToken = "auth-token";
const accessToken = "access-token";
const emailAddressField = getByLabelText("Email Address");
const passwordField = getByLabelText("Password");
const submitButton = form!.find(
@@ -185,7 +185,7 @@ it("submits form successfully", async () => {
},
})
.once()
.returns({ token: authToken });
.returns({ token: accessToken });
form!.props.onSubmit();
@@ -198,7 +198,7 @@ it("submits form successfully", async () => {
expect(toJSON(form!)).toMatchSnapshot();
// Wait for window hash to contain a token.
await wait(() => expect(location.hash).toBe(`#accessToken=${authToken}`));
await wait(() => expect(location.hash).toBe(`#accessToken=${accessToken}`));
restMock.verify();
});
+3 -3
View File
@@ -186,7 +186,7 @@ it("shows server error", async () => {
it("submits form successfully", async () => {
const { context, main, form } = await createTestRenderer();
const { getByLabelText } = within(form!);
const authToken = "auth-token";
const accessToken = "access-token";
const emailAddressField = getByLabelText("Email Address");
const usernameField = getByLabelText("Username");
const passwordField = getByLabelText("Password");
@@ -210,7 +210,7 @@ it("submits form successfully", async () => {
},
})
.once()
.returns({ token: authToken });
.returns({ token: accessToken });
form!.props.onSubmit();
@@ -224,7 +224,7 @@ it("submits form successfully", async () => {
expect(toJSON(main)).toMatchSnapshot();
// Wait for window hash to contain a token.
await wait(() => expect(location.hash).toBe(`#accessToken=${authToken}`));
await wait(() => expect(location.hash).toBe(`#accessToken=${accessToken}`));
restMock.verify();
});
+2 -2
View File
@@ -30,7 +30,7 @@ export interface StreamEmbedConfig {
eventEmitter: EventEmitter2;
id: string;
rootURL: string;
authToken?: string;
accessToken?: string;
}
export class StreamEmbed {
@@ -123,7 +123,7 @@ export class StreamEmbed {
}
const externalConfig: ExternalConfig = {
authToken: this.config.authToken,
accessToken: this.config.accessToken,
};
const streamDecorators: ReadonlyArray<Decorator> = [
+2 -2
View File
@@ -12,7 +12,7 @@ export interface Config {
id?: string;
autoRender?: boolean;
events?: (eventEmitter: EventEmitter2) => void;
authToken?: string;
accessToken?: string;
}
function getLocationOrigin() {
@@ -58,6 +58,6 @@ export function createStreamEmbed(config: Config): StreamEmbed {
rootURL: config.rootURL || getLocationOrigin(),
autoRender: config.autoRender,
eventEmitter,
authToken: config.authToken,
accessToken: config.accessToken,
});
}
@@ -3,7 +3,7 @@ import sinon from "sinon";
import withConfig from "./withConfig";
it("should emit events from pym to Config", () => {
const config = { authToken: "token" };
const config = { accessToken: "token" };
const fakePym = {
onMessage: (type: string, callback: () => void) => {
expect(type).toBe("getConfig");
@@ -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(
+1 -1
View File
@@ -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(
@@ -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);
});
@@ -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);
+4 -4
View File
@@ -1,8 +1,8 @@
export {
withSetAuthTokenMutation,
SetAuthTokenMutation,
SetAuthTokenInput,
} from "./SetAuthTokenMutation";
withSetAccessTokenMutation,
SetAccessTokenMutation,
SetAccessTokenInput,
} from "./SetAccessTokenMutation";
export { withSignOutMutation, SignOutMutation } from "./SignOutMutation";
export {
withSetNetworkStatusMutation,
@@ -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";
@@ -17,7 +17,7 @@ it("renders fully", () => {
focus: false,
view: "SIGN_IN",
},
authJTI: "JTI",
accessTokenJTI: "JTI",
},
me: null,
settings: {
@@ -74,7 +74,7 @@ it("renders without logout button", () => {
focus: false,
view: "SIGN_IN",
},
authJTI: null,
accessTokenJTI: null,
},
me: null,
settings: {
@@ -131,7 +131,7 @@ it("renders sso only", () => {
focus: false,
view: "SIGN_IN",
},
authJTI: "JTI",
accessTokenJTI: "JTI",
},
me: null,
settings: {
@@ -188,7 +188,7 @@ it("renders sso only without logout button", () => {
focus: false,
view: "SIGN_IN",
},
authJTI: "JTI",
accessTokenJTI: "JTI",
},
me: null,
settings: {
@@ -245,7 +245,7 @@ it("renders without register button", () => {
focus: false,
view: "SIGN_IN",
},
authJTI: "JTI",
accessTokenJTI: "JTI",
},
me: null,
settings: {
@@ -40,7 +40,7 @@ export class UserBoxContainer extends Component<InnerProps> {
private handleSignOut = () => this.props.signOut();
private get supportsLogout() {
return !!this.props.local.authJTI;
return !!this.props.local.accessTokenJTI;
}
private get supportsRegister() {
@@ -120,7 +120,7 @@ const enhanced = withSignOutMutation(
focus
view
}
authJTI
accessTokenJTI
}
`
)(
+2 -2
View File
@@ -8,7 +8,7 @@ import AppContainer from "talk-stream/containers/AppContainer";
import {
OnEvents,
OnPostMessageSetAuthToken,
OnPostMessageSetAccessToken,
OnPymLogin,
OnPymLogout,
OnPymSetCommentID,
@@ -21,7 +21,7 @@ const listeners = (
<OnPymLogin />
<OnPymLogout />
<OnPymSetCommentID />
<OnPostMessageSetAuthToken />
<OnPostMessageSetAccessToken />
<OnEvents />
</>
);
@@ -3,27 +3,27 @@ import { createRenderer } from "react-test-renderer/shallow";
import { createSinonStub } from "talk-framework/testHelpers";
import { OnPostMessageSetAuthToken } from "./OnPostMessageSetAuthToken";
import { OnPostMessageSetAccessToken } from "./OnPostMessageSetAccessToken";
it("Listens to event and sets auth token", () => {
const token = "auth-token";
const token = "access-token";
const postMessage: any = {
on: (name: string, cb: (token: string) => void) => {
expect(name).toBe("setAuthToken");
expect(name).toBe("setAccessToken");
cb(token);
},
};
const setAuthToken = createSinonStub(
const setAccessToken = createSinonStub(
s => s.throws(),
s => s.withArgs({ authToken: token }).returns(null)
s => s.withArgs({ accessToken: token }).returns(null)
);
createRenderer().render(
<OnPostMessageSetAuthToken
<OnPostMessageSetAccessToken
postMessage={postMessage}
setAuthToken={setAuthToken}
setAccessToken={setAccessToken}
/>
);
expect(setAuthToken.calledOnce);
expect(setAccessToken.calledOnce);
});
@@ -2,21 +2,21 @@ import { Component } from "react";
import { TalkContext, withContext } from "talk-framework/lib/bootstrap";
import {
SetAuthTokenMutation,
withSetAuthTokenMutation,
} from "talk-framework/mutations/SetAuthTokenMutation";
SetAccessTokenMutation,
withSetAccessTokenMutation,
} from "talk-framework/mutations/SetAccessTokenMutation";
interface Props {
postMessage: TalkContext["postMessage"];
setAuthToken: SetAuthTokenMutation;
setAccessToken: SetAccessTokenMutation;
}
export class OnPostMessageSetAuthToken extends Component<Props> {
export class OnPostMessageSetAccessToken extends Component<Props> {
constructor(props: Props) {
super(props);
// Auth popup will use this to handle a successful login.
props.postMessage!.on("setAuthToken", (authToken: string) => {
props.setAuthToken({ authToken });
props.postMessage!.on("setAccessToken", (accessToken: string) => {
props.setAccessToken({ accessToken });
});
}
@@ -26,6 +26,6 @@ export class OnPostMessageSetAuthToken extends Component<Props> {
}
const enhanced = withContext(({ postMessage }) => ({ postMessage }))(
withSetAuthTokenMutation(OnPostMessageSetAuthToken)
withSetAccessTokenMutation(OnPostMessageSetAccessToken)
);
export default enhanced;
@@ -5,20 +5,22 @@ import { createSinonStub } from "talk-framework/testHelpers";
import { OnPymLogin } from "./OnPymLogin";
it("Listens to event and calls setAuthToken", () => {
const authToken = "auth-token";
it("Listens to event and calls setAccessToken", () => {
const accessToken = "access-token";
const pym: any = {
onMessage: (eventName: string, cb: (authToken: string) => void) => {
onMessage: (eventName: string, cb: (accessToken: string) => void) => {
expect(eventName).toBe("login");
cb(authToken);
cb(accessToken);
},
};
const setAuthToken = createSinonStub(
const setAccessToken = createSinonStub(
s => s.throws(),
s => s.withArgs({ authToken }).returns(null)
s => s.withArgs({ accessToken }).returns(null)
);
createRenderer().render(<OnPymLogin pym={pym} setAuthToken={setAuthToken} />);
expect(setAuthToken.calledOnce);
createRenderer().render(
<OnPymLogin pym={pym} setAccessToken={setAccessToken} />
);
expect(setAccessToken.calledOnce);
});
@@ -1,15 +1,15 @@
import { Child } from "pym.js";
import { Component } from "react";
import {
SetAuthTokenMutation,
withSetAuthTokenMutation,
SetAccessTokenMutation,
withSetAccessTokenMutation,
} from "talk-framework/mutations";
import { withContext } from "talk-framework/lib/bootstrap";
interface Props {
pym: Child;
setAuthToken: SetAuthTokenMutation;
setAccessToken: SetAccessTokenMutation;
}
export class OnPymLogin extends Component<Props> {
@@ -17,8 +17,8 @@ export class OnPymLogin extends Component<Props> {
super(props);
// Sets comment id through pym.
props.pym!.onMessage("login", authToken => {
this.props.setAuthToken({ authToken });
props.pym!.onMessage("login", accessToken => {
this.props.setAccessToken({ accessToken });
});
}
@@ -29,6 +29,6 @@ export class OnPymLogin extends Component<Props> {
const enhanced = withContext(({ pym }) => ({
pym,
}))(withSetAuthTokenMutation(OnPymLogin));
}))(withSetAccessTokenMutation(OnPymLogin));
export default enhanced;
@@ -5,11 +5,11 @@ import sinon from "sinon";
import { OnPymLogout } from "./OnPymLogout";
it("Listens to event and calls signOut", () => {
const authToken = "auth-token";
const accessToken = "access-token";
const pym: any = {
onMessage: (eventName: string, cb: (authToken: string) => void) => {
onMessage: (eventName: string, cb: (accessToken: string) => void) => {
expect(eventName).toBe("logout");
cb(authToken);
cb(accessToken);
},
};
@@ -14,7 +14,7 @@ export class OnPymLogout extends Component<Props> {
super(props);
// Sets comment id through pym.
props.pym!.onMessage("logout", authToken => {
props.pym!.onMessage("logout", accessToken => {
this.props.signOut();
});
}
+2 -2
View File
@@ -1,7 +1,7 @@
export { default as OnPymSetCommentID } from "./OnPymSetCommentID";
export {
default as OnPostMessageSetAuthToken,
} from "./OnPostMessageSetAuthToken";
default as OnPostMessageSetAccessToken,
} from "./OnPostMessageSetAccessToken";
export { default as OnEvents } from "./OnEvents";
export { default as OnPymLogin } from "./OnPymLogin";
export { default as OnPymLogout } from "./OnPymLogout";
@@ -12,9 +12,9 @@ exports[`init local state 1`] = `
\\"client:root.local\\": {
\\"__id\\": \\"client:root.local\\",
\\"__typename\\": \\"Local\\",
\\"authToken\\": \\"\\",
\\"authExp\\": null,
\\"authJTI\\": null,
\\"accessToken\\": \\"\\",
\\"accessTokenExp\\": null,
\\"accessTokenJTI\\": null,
\\"loggedIn\\": false,
\\"network\\": {
\\"__ref\\": \\"client:root.local.network\\"
@@ -5,7 +5,7 @@ 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,
replaceHistoryLocation,
} from "talk-framework/testHelpers";
@@ -58,12 +58,12 @@ it("set commentID from query", async () => {
restoreHistoryLocation();
});
it("set authToken from localStorage", async () => {
it("set accessToken from localStorage", async () => {
const context: Partial<TalkContext> = {
localStorage: createPromisifiedStorage(),
};
const authToken = createAuthToken();
context.localStorage!.setItem("authToken", authToken);
const accessToken = createAccessToken();
context.localStorage!.setItem("accessToken", accessToken);
await initLocalState(environment, context as any);
expect(source.get(LOCAL_ID)!.authToken).toBe(authToken);
expect(source.get(LOCAL_ID)!.accessToken).toBe(accessToken);
});
@@ -18,7 +18,7 @@ export default async function initLocalState(
await initLocalBaseState(
environment,
context,
config ? config.authToken : undefined
config ? config.accessToken : undefined
);
commitLocalUpdate(environment, s => {
+3 -3
View File
@@ -29,9 +29,9 @@ extend type Comment {
type Local {
network: Network!
authToken: String
authExp: Int
authJTI: String
accessToken: String
accessTokenExp: Int
accessTokenJTI: String
loggedIn: Boolean!
activeTab: Tab!
authPopup: AuthPopup!
@@ -7,7 +7,7 @@ export default function create(params: CreateParams) {
if (params.initLocalState) {
localRecord.setValue("COMMENTS", "activeTab");
localRecord.setValue(false, "loggedIn");
localRecord.setValue("jti", "authJTI");
localRecord.setValue("jti", "accessTokenJTI");
params.initLocalState(localRecord, source, environment);
}
},
@@ -6,7 +6,7 @@ export default function create(params: CreateParams) {
initLocalState: (localRecord, source, environment) => {
if (params.initLocalState) {
localRecord.setValue("PROFILE", "activeTab");
localRecord.setValue("jti", "authJTI");
localRecord.setValue("jti", "accessTokenJTI");
params.initLocalState(localRecord, source, environment);
}
},