mirror of
https://github.com/wassname/talk.git
synced 2026-08-02 13:10:23 +08:00
Support rest and implement SignIn and SignUp Rest + Mutations
This commit is contained in:
@@ -21,7 +21,7 @@ const handleResp = (res: Response) => {
|
||||
if (res.status > 399) {
|
||||
return res.json().then((err: any) => {
|
||||
// TODO: sync error handling with server.
|
||||
const message = err.message || res.status;
|
||||
const message = err.message || err.error || res.status;
|
||||
const error = new Error(message);
|
||||
throw error;
|
||||
});
|
||||
@@ -43,7 +43,7 @@ export class RestClient {
|
||||
this.tokenGetter = tokenGetter;
|
||||
}
|
||||
|
||||
public fetch(path: string, options: PartialRequestInit): Promise<Response> {
|
||||
public fetch<T>(path: string, options: PartialRequestInit): Promise<T> {
|
||||
let opts = options;
|
||||
if (this.tokenGetter) {
|
||||
opts = merge({}, options, {
|
||||
|
||||
@@ -2,6 +2,7 @@ import { commitLocalUpdate, Environment, RecordSource } from "relay-runtime";
|
||||
|
||||
import { timeout } from "talk-common/utils";
|
||||
import { LOCAL_ID } from "talk-framework/lib/relay";
|
||||
import { createInMemoryStorage } from "talk-framework/lib/storage";
|
||||
import { createRelayEnvironment } from "talk-framework/testHelpers";
|
||||
|
||||
import { commit } from "./SetAuthTokenMutation";
|
||||
@@ -16,18 +17,34 @@ beforeAll(() => {
|
||||
});
|
||||
|
||||
it("Sets auth token", async () => {
|
||||
const context = {
|
||||
localStorage: createInMemoryStorage(),
|
||||
};
|
||||
const authToken = "auth token";
|
||||
commit(environment, { authToken });
|
||||
commit(environment, { authToken }, context as any);
|
||||
expect(source.get(LOCAL_ID)!.authToken).toEqual(authToken);
|
||||
expect(context.localStorage.getItem("authToken")).toEqual(authToken);
|
||||
});
|
||||
|
||||
it("Removes auth token from localStorage", async () => {
|
||||
const context = {
|
||||
localStorage: createInMemoryStorage(),
|
||||
};
|
||||
localStorage.setItem("authToken", "tmp");
|
||||
commit(environment, { authToken: null }, context as any);
|
||||
expect(context.localStorage.getItem("authToken")).toBeUndefined();
|
||||
});
|
||||
|
||||
it("Should call gc", async () => {
|
||||
const context = {
|
||||
localStorage: createInMemoryStorage(),
|
||||
};
|
||||
commitLocalUpdate(environment, store => {
|
||||
store.create("should-disappear", "tmp");
|
||||
});
|
||||
const authToken = null;
|
||||
expect(source.get("should-disappear")).not.toBeUndefined();
|
||||
commit(environment, { authToken });
|
||||
commit(environment, { authToken }, context as any);
|
||||
await timeout();
|
||||
expect(source.get(LOCAL_ID)!.authToken).toEqual(authToken);
|
||||
expect(source.get("should-disappear")).toBeUndefined();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { commitLocalUpdate, Environment } from "relay-runtime";
|
||||
|
||||
import { TalkContext } from "talk-framework/lib/bootstrap";
|
||||
import { createMutationContainer } from "talk-framework/lib/relay";
|
||||
import { LOCAL_ID } from "talk-framework/lib/relay/withLocalStateContainer";
|
||||
|
||||
@@ -11,11 +12,17 @@ export type SetAuthTokenMutation = (input: SetAuthTokenInput) => Promise<void>;
|
||||
|
||||
export async function commit(
|
||||
environment: Environment,
|
||||
input: SetAuthTokenInput
|
||||
input: SetAuthTokenInput,
|
||||
{ localStorage }: TalkContext
|
||||
) {
|
||||
return commitLocalUpdate(environment, store => {
|
||||
const record = store.get(LOCAL_ID)!;
|
||||
record.setValue(input.authToken, "authToken");
|
||||
if (input.authToken) {
|
||||
localStorage.setItem("authToken", input.authToken);
|
||||
} else {
|
||||
localStorage.removeItem("authToken");
|
||||
}
|
||||
|
||||
// Force gc to trigger.
|
||||
environment
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
import { RestClient } from "../lib/rest";
|
||||
|
||||
export interface SignInInput {
|
||||
username: string;
|
||||
email: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export interface SignInResponse {
|
||||
token: string;
|
||||
}
|
||||
|
||||
export default function signIn(rest: RestClient, input: SignInInput) {
|
||||
return rest.fetch("/tenant/auth/local", {
|
||||
return rest.fetch<SignInResponse>("/tenant/auth/local", {
|
||||
method: "POST",
|
||||
body: input,
|
||||
});
|
||||
|
||||
@@ -6,8 +6,12 @@ export interface SignUpInput {
|
||||
email: string;
|
||||
}
|
||||
|
||||
export interface SignUpResponse {
|
||||
token: string;
|
||||
}
|
||||
|
||||
export default function signUp(rest: RestClient, input: SignUpInput) {
|
||||
return rest.fetch("/tenant/auth/local/signup", {
|
||||
return rest.fetch<SignUpResponse>("/tenant/auth/local/signup", {
|
||||
method: "POST",
|
||||
body: input,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user