sdy

update Auth Queries and useMutation hook

......@@ -15,40 +15,36 @@ export default () => {
const username = useInput("");
// mutations
const loginMutation = useMutation(LOGIN, {
variables: { email: email.value, password: password.value },
});
const [login] = useMutation(LOGIN);
const createAccountMutation = useMutation(SIGNUP, {
variables: {
email: email.value,
username: username.value,
password: password.value,
password2: password2.value,
phoneNum: phoneNum.value,
},
});
const [createAccount] = useMutation(SIGNUP);
// TODO: make login success query.
// TODO: When login or signup success, get token from mutation function
let Auth;
let token;
const onSubmit = async (e) => {
e.preventDefault();
if (action === "logIn") {
if (email.value !== "") {
try {
// QL 을 통해서 data 를 가져온다.
const {
data: { login }, // AuthQueries 에 정의된 mutation 값
} = await loginMutation();
if (!login) {
Auth = await login({
variables: { email: email.value, password: password.value },
});
token = Auth.data.login.token;
if (!Auth) {
// when login fail
toast.warn("you need to make your own account");
setAction("signUp");
} else {
// when login success
toast.success("login success");
setAction("confirm");
// TODO: move page to Main container.
}
} catch (e) {}
} catch {
toast.error("login failed!");
}
} else {
// TODO: inform "email is required" using toastify
toast.error("email is required!", {
position: "top-center",
autoClose: 3000,
......@@ -65,26 +61,29 @@ export default () => {
phoneNum.value !== "")
) {
try {
const {
data: { signUp },
} = await createAccountMutation();
if (!signUp) {
Auth = await createAccount({
variables: {
email: email.value,
username: username.value,
password: password.value,
password2: password2.value,
phoneNum: phoneNum.value,
},
});
token = Auth.data.createAccount.token;
if (!Auth) {
toast.warn("you need to sign up first");
setAction("signUp");
} else {
toast.success("login success");
setAction("logIn");
}
} catch (e) {
// TODO: can't find data: signUp
} catch {
toast.error("can't sign up, please check input data");
}
} else {
// TODO: inform user need to input values that required for sign up with toastify
toast.error("you need to input with vaild data");
}
} else if (action === "confirm") {
// TODO: when login success, go to Main Container
}
};
......
......@@ -2,24 +2,34 @@ import { gql } from "apollo-boost";
export const LOGIN = gql`
mutation login($email: String!, $password: String!) {
login(email: $email, password: $password)
login(email: $email, password: $password) {
token
user {
id
}
}
}
`;
export const SIGNUP = gql`
mutation signUp(
mutation createAccount(
$email: String!
$username: String!
$password: String!
$password2: String!
$phoneNum: String!
) {
signUp(
createAccount(
email: $email
username: $username
password: $password
password2: $password2
phoneNum: $phoneNum
)
) {
token
user {
id
}
}
}
`;
......