sdy

update AuthContainer.js

1 import React, { useState } from "react"; 1 import React, { useState } from "react";
2 import { useMutation } from "@apollo/react-hooks"; 2 import { useMutation } from "@apollo/react-hooks";
3 -import { LOGIN, SIGNUP } from "./AuthQueries"; 3 +import { LOGIN, SIGNUP, LOCAL_LOG_IN } from "./AuthQueries";
4 import useInput from "../../Hooks/useInput"; 4 import useInput from "../../Hooks/useInput";
5 import { toast } from "react-toastify"; 5 import { toast } from "react-toastify";
6 import AuthPresenter from "./AuthPresenter"; 6 import AuthPresenter from "./AuthPresenter";
...@@ -14,15 +14,12 @@ export default () => { ...@@ -14,15 +14,12 @@ export default () => {
14 const phoneNum = useInput(""); 14 const phoneNum = useInput("");
15 const username = useInput(""); 15 const username = useInput("");
16 16
17 - // mutations
18 const [login] = useMutation(LOGIN); 17 const [login] = useMutation(LOGIN);
19 -
20 const [createAccount] = useMutation(SIGNUP); 18 const [createAccount] = useMutation(SIGNUP);
19 + const [loginState] = useMutation(LOCAL_LOG_IN);
21 20
22 - // TODO: When login or signup success, get token from mutation function 21 + let Auth; // mutation result variable
23 - 22 + let token; // token from login or signUp.
24 - let Auth;
25 - let token;
26 const onSubmit = async (e) => { 23 const onSubmit = async (e) => {
27 e.preventDefault(); 24 e.preventDefault();
28 if (action === "logIn") { 25 if (action === "logIn") {
...@@ -30,7 +27,7 @@ export default () => { ...@@ -30,7 +27,7 @@ export default () => {
30 try { 27 try {
31 Auth = await login({ 28 Auth = await login({
32 variables: { email: email.value, password: password.value }, 29 variables: { email: email.value, password: password.value },
33 - }); 30 + }); // AuthPayload object.
34 token = Auth.data.login.token; 31 token = Auth.data.login.token;
35 if (!Auth) { 32 if (!Auth) {
36 // when login fail 33 // when login fail
...@@ -38,8 +35,11 @@ export default () => { ...@@ -38,8 +35,11 @@ export default () => {
38 setAction("signUp"); 35 setAction("signUp");
39 } else { 36 } else {
40 // when login success 37 // when login success
38 + console.log("login success");
41 toast.success("login success"); 39 toast.success("login success");
42 - // TODO: move page to Main container. 40 + await loginState({
41 + variables: { token },
42 + });
43 } 43 }
44 } catch { 44 } catch {
45 toast.error("login failed!"); 45 toast.error("login failed!");
......