Skip to content

Commit

Permalink
refactor(mentor): api 통신 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
HaydenDevK committed Oct 18, 2023
1 parent 0bc5b12 commit d765a97
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 95 deletions.
74 changes: 0 additions & 74 deletions utils/api.js

This file was deleted.

21 changes: 21 additions & 0 deletions utils/apiClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { BASE_URL } from "./constants.js";

export const fetchClient = async ({ url, method, body }) => {
try {
const response = await fetch(`${BASE_URL}/${url}`, {
method,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});

if (response.status !== 200) {
throw new Error(response.status);
} else {
return response;
}
} catch (error) {
throw error;
}
};
42 changes: 42 additions & 0 deletions utils/auth.js → utils/authorize.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
FOLDER_PAGE_PATH,
} from "/utils/constants.js";

import { fetchClient } from "./apiClient.js";

// 비밀번호 토글
function getPasswordVisibility(inputType) {
return inputType === "password"
Expand Down Expand Up @@ -77,6 +79,43 @@ function getIsConfirmedConfirmPassword(
}
}

const signIn = async (email, password) => {
const response = await fetchClient({
url: "sign-in",
method: "POST",
body: { email, password },
});
const result = await response.json();
const accessToken = result.data.accessToken;
window.localStorage.setItem("accessToken", accessToken);
goToFolderPage();
};

const getIsNewEmail = async (email) => {
try {
await fetchClient({
url: "check-email",
method: "POST",
body: { email },
});
return true;
} catch {
return false;
}
};

const signUp = async (email, password) => {
const response = await fetchClient({
url: "sign-up",
method: "POST",
body: { email, password },
});
const result = await response.json();
const accessToken = result.data.accessToken;
window.localStorage.setItem("accessToken", accessToken);
goToFolderPage();
};

export {
getPasswordVisibility,
goToFolderPage,
Expand All @@ -87,4 +126,7 @@ export {
getIsCorrectPassword,
getIsFilledConfirmPassword,
getIsConfirmedConfirmPassword,
signIn,
getIsNewEmail,
signUp,
};
10 changes: 4 additions & 6 deletions utils/signin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import {
getIsFilledEmail,
getIsValidEmail,
getIsFilledPassword,
} from "/utils/auth.js";
signIn,
} from "/utils/authorize.js";

import {
AUTH_HINT,
INPUT_STATUS,
INPUT_HINT_CLASSNAME,
} from "/utils/constants.js";

import { signIn } from "./api.js";

/* 로그인 상태로 접근 시 리다이렉트 */
(function () {
const accessToken = localStorage.getItem("accessToken");
Expand Down Expand Up @@ -106,10 +105,9 @@ function getIsCompletePassword(password) {
return true;
}
}

async function clickSignin(email, password) {
function clickSignin(email, password) {
if (getIsCompleteEmail(email) && getIsCompletePassword(password))
await signIn({ email, password });
signIn(email, password);
}

emailInputElement.addEventListener("focusout", (e) => {
Expand Down
20 changes: 5 additions & 15 deletions utils/signup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ import {
getIsValidPassword,
getIsFilledConfirmPassword,
getIsConfirmedConfirmPassword,
} from "/utils/auth.js";
signUp,
getIsNewEmail,
} from "/utils/authorize.js";

import {
AUTH_HINT,
INPUT_STATUS,
INPUT_HINT_CLASSNAME,
} from "/utils/constants.js";

import { signUp } from "./api.js";

import { getIsNewEmail } from "/utils/api.js";

/* 로그인 상태로 접근 시 리다이렉트 */
(function () {
const accessToken = localStorage.getItem("accessToken");
Expand Down Expand Up @@ -186,13 +184,13 @@ function getIsConfirmedPassword(confirmPassword) {
}
}

async function clickSignup({ email, password, confirmPassword }) {
function clickSignup({ email, password, confirmPassword }) {
if (
getIsCompleteEmail(email) &&
getIsCompletePassword(password) &&
getIsConfirmedPassword(confirmPassword)
)
await signUp({ email, password });
signUp(email, password);
}

emailInputElement.addEventListener("focusout", (e) => {
Expand All @@ -207,14 +205,6 @@ confirmPasswordInputElement.addEventListener("focusout", (e) => {
checkPasswordConfirmFocusout(e.target.value);
});

emailInputElement.addEventListener("focusout", (e) => {
checkEmailFocusout(e.target.value);
});

passwordInputElement.addEventListener("focusout", (e) => {
checkPasswordFocusout(e.target.value);
});

signupButtonElement.addEventListener("click", (e) => {
e.preventDefault();
clickSignup({
Expand Down

0 comments on commit d765a97

Please sign in to comment.