sdy

update Auth Queries and useMutation hook

...@@ -15,40 +15,36 @@ export default () => { ...@@ -15,40 +15,36 @@ export default () => {
15 const username = useInput(""); 15 const username = useInput("");
16 16
17 // mutations 17 // mutations
18 - const loginMutation = useMutation(LOGIN, { 18 + const [login] = useMutation(LOGIN);
19 - variables: { email: email.value, password: password.value },
20 - });
21 19
22 - const createAccountMutation = useMutation(SIGNUP, { 20 + const [createAccount] = useMutation(SIGNUP);
23 - variables: {
24 - email: email.value,
25 - username: username.value,
26 - password: password.value,
27 - password2: password2.value,
28 - phoneNum: phoneNum.value,
29 - },
30 - });
31 21
32 - // TODO: make login success query. 22 + // TODO: When login or signup success, get token from mutation function
33 23
24 + let Auth;
25 + let token;
34 const onSubmit = async (e) => { 26 const onSubmit = async (e) => {
27 + e.preventDefault();
35 if (action === "logIn") { 28 if (action === "logIn") {
36 if (email.value !== "") { 29 if (email.value !== "") {
37 try { 30 try {
38 - // QL 을 통해서 data 를 가져온다. 31 + Auth = await login({
39 - const { 32 + variables: { email: email.value, password: password.value },
40 - data: { login }, // AuthQueries 에 정의된 mutation 값 33 + });
41 - } = await loginMutation(); 34 + token = Auth.data.login.token;
42 - if (!login) { 35 + if (!Auth) {
36 + // when login fail
43 toast.warn("you need to make your own account"); 37 toast.warn("you need to make your own account");
44 setAction("signUp"); 38 setAction("signUp");
45 } else { 39 } else {
40 + // when login success
46 toast.success("login success"); 41 toast.success("login success");
47 - setAction("confirm"); 42 + // TODO: move page to Main container.
48 } 43 }
49 - } catch (e) {} 44 + } catch {
45 + toast.error("login failed!");
46 + }
50 } else { 47 } else {
51 - // TODO: inform "email is required" using toastify
52 toast.error("email is required!", { 48 toast.error("email is required!", {
53 position: "top-center", 49 position: "top-center",
54 autoClose: 3000, 50 autoClose: 3000,
...@@ -65,26 +61,29 @@ export default () => { ...@@ -65,26 +61,29 @@ export default () => {
65 phoneNum.value !== "") 61 phoneNum.value !== "")
66 ) { 62 ) {
67 try { 63 try {
68 - const { 64 + Auth = await createAccount({
69 - data: { signUp }, 65 + variables: {
70 - } = await createAccountMutation(); 66 + email: email.value,
71 - if (!signUp) { 67 + username: username.value,
68 + password: password.value,
69 + password2: password2.value,
70 + phoneNum: phoneNum.value,
71 + },
72 + });
73 + token = Auth.data.createAccount.token;
74 + if (!Auth) {
72 toast.warn("you need to sign up first"); 75 toast.warn("you need to sign up first");
73 setAction("signUp"); 76 setAction("signUp");
74 } else { 77 } else {
75 toast.success("login success"); 78 toast.success("login success");
76 setAction("logIn"); 79 setAction("logIn");
77 } 80 }
78 - } catch (e) { 81 + } catch {
79 - // TODO: can't find data: signUp
80 toast.error("can't sign up, please check input data"); 82 toast.error("can't sign up, please check input data");
81 } 83 }
82 } else { 84 } else {
83 - // TODO: inform user need to input values that required for sign up with toastify
84 toast.error("you need to input with vaild data"); 85 toast.error("you need to input with vaild data");
85 } 86 }
86 - } else if (action === "confirm") {
87 - // TODO: when login success, go to Main Container
88 } 87 }
89 }; 88 };
90 89
......
...@@ -2,24 +2,34 @@ import { gql } from "apollo-boost"; ...@@ -2,24 +2,34 @@ import { gql } from "apollo-boost";
2 2
3 export const LOGIN = gql` 3 export const LOGIN = gql`
4 mutation login($email: String!, $password: String!) { 4 mutation login($email: String!, $password: String!) {
5 - login(email: $email, password: $password) 5 + login(email: $email, password: $password) {
6 + token
7 + user {
8 + id
9 + }
10 + }
6 } 11 }
7 `; 12 `;
8 13
9 export const SIGNUP = gql` 14 export const SIGNUP = gql`
10 - mutation signUp( 15 + mutation createAccount(
11 $email: String! 16 $email: String!
12 $username: String! 17 $username: String!
13 $password: String! 18 $password: String!
14 $password2: String! 19 $password2: String!
15 $phoneNum: String! 20 $phoneNum: String!
16 ) { 21 ) {
17 - signUp( 22 + createAccount(
18 email: $email 23 email: $email
19 username: $username 24 username: $username
20 password: $password 25 password: $password
21 password2: $password2 26 password2: $password2
22 phoneNum: $phoneNum 27 phoneNum: $phoneNum
23 - ) 28 + ) {
29 + token
30 + user {
31 + id
32 + }
33 + }
24 } 34 }
25 `; 35 `;
......