mirror of
https://github.com/wassname/talk.git
synced 2026-07-26 13:37:38 +08:00
Support authentication
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import { commitLocalUpdate, Environment, RecordSource } from "relay-runtime";
|
||||
|
||||
import { timeout } from "talk-common/utils";
|
||||
import { LOCAL_ID } from "talk-framework/lib/relay";
|
||||
import { createRelayEnvironment } from "talk-framework/testHelpers";
|
||||
|
||||
import { commit } from "./SetAuthTokenMutation";
|
||||
|
||||
let environment: Environment;
|
||||
const source: RecordSource = new RecordSource();
|
||||
|
||||
beforeAll(() => {
|
||||
environment = createRelayEnvironment({
|
||||
source,
|
||||
});
|
||||
});
|
||||
|
||||
it("Sets auth token", async () => {
|
||||
const authToken = "auth token";
|
||||
commit(environment, { authToken });
|
||||
expect(source.get(LOCAL_ID)!.authToken).toEqual(authToken);
|
||||
});
|
||||
|
||||
it("Should call gc", async () => {
|
||||
commitLocalUpdate(environment, store => {
|
||||
store.create("should-disappear", "tmp");
|
||||
});
|
||||
const authToken = null;
|
||||
expect(source.get("should-disappear")).not.toBeUndefined();
|
||||
commit(environment, { authToken });
|
||||
await timeout();
|
||||
expect(source.get(LOCAL_ID)!.authToken).toEqual(authToken);
|
||||
expect(source.get("should-disappear")).toBeUndefined();
|
||||
});
|
||||
@@ -0,0 +1,34 @@
|
||||
import { commitLocalUpdate, Environment } from "relay-runtime";
|
||||
|
||||
import { createMutationContainer } from "talk-framework/lib/relay";
|
||||
import { LOCAL_ID } from "talk-framework/lib/relay/withLocalStateContainer";
|
||||
|
||||
export interface SetAuthTokenInput {
|
||||
authToken: string | null;
|
||||
}
|
||||
|
||||
export type SetAuthTokenMutation = (input: SetAuthTokenInput) => Promise<void>;
|
||||
|
||||
export async function commit(
|
||||
environment: Environment,
|
||||
input: SetAuthTokenInput
|
||||
) {
|
||||
return commitLocalUpdate(environment, store => {
|
||||
const record = store.get(LOCAL_ID)!;
|
||||
record.setValue(input.authToken, "authToken");
|
||||
|
||||
// Force gc to trigger.
|
||||
environment
|
||||
.retain({
|
||||
dataID: "tmp",
|
||||
node: { selections: [] },
|
||||
variables: {},
|
||||
})
|
||||
.dispose();
|
||||
});
|
||||
}
|
||||
|
||||
export const withSetAuthTokenMutation = createMutationContainer(
|
||||
"setCommentID",
|
||||
commit
|
||||
);
|
||||
@@ -0,0 +1,5 @@
|
||||
export {
|
||||
withSetAuthTokenMutation,
|
||||
SetAuthTokenMutation,
|
||||
SetAuthTokenInput,
|
||||
} from "./SetAuthTokenMutation";
|
||||
Reference in New Issue
Block a user