sdy

update AuthContainer.js

import React, { useState } from "react";
import { useMutation } from "@apollo/react-hooks";
import { LOGIN, SIGNUP } from "./AuthQueries";
import { LOGIN, SIGNUP, LOCAL_LOG_IN } from "./AuthQueries";
import useInput from "../../Hooks/useInput";
import { toast } from "react-toastify";
import AuthPresenter from "./AuthPresenter";
......@@ -14,15 +14,12 @@ export default () => {
const phoneNum = useInput("");
const username = useInput("");
// mutations
const [login] = useMutation(LOGIN);
const [createAccount] = useMutation(SIGNUP);
const [loginState] = useMutation(LOCAL_LOG_IN);
// TODO: When login or signup success, get token from mutation function
let Auth;
let token;
let Auth; // mutation result variable
let token; // token from login or signUp.
const onSubmit = async (e) => {
e.preventDefault();
if (action === "logIn") {
......@@ -30,7 +27,7 @@ export default () => {
try {
Auth = await login({
variables: { email: email.value, password: password.value },
});
}); // AuthPayload object.
token = Auth.data.login.token;
if (!Auth) {
// when login fail
......@@ -38,8 +35,11 @@ export default () => {
setAction("signUp");
} else {
// when login success
console.log("login success");
toast.success("login success");
// TODO: move page to Main container.
await loginState({
variables: { token },
});
}
} catch {
toast.error("login failed!");
......