diff --git a/utils/api.js b/utils/api.js deleted file mode 100644 index 35087d6ff..000000000 --- a/utils/api.js +++ /dev/null @@ -1,74 +0,0 @@ -import { BASE_URL } from "./constants.js"; -import { goToFolderPage } from "./auth.js"; - -async function signIn({ email, password }) { - try { - const response = await fetch(`${BASE_URL}/sign-in`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ email, password }), - }); - - if (!response.ok) { - // Q1. 필요한가 - throw new Error(response.status); - } else { - const result = await response.json(); - const accessToken = result.data.accessToken; - window.localStorage.setItem("accessToken", accessToken); - goToFolderPage(); - } - } catch (error) { - console.error("로그인 실패:", error); - } -} - -async function getIsNewEmail(email) { - try { - const response = await fetch(`${BASE_URL}/check-email`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ email }), - }); - - if (!response.ok) { - // Q1. 필요한가 - throw new Error(response.status); - } else { - return true; - } - } catch (error) { - console.error(error); - return false; - } -} - -async function signUp({ email, password }) { - try { - const response = await fetch(`${BASE_URL}/sign-up`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ email, password }), - }); - - if (!response.ok) { - // Q1. 필요한가 - throw new Error(response.status); - } else { - const result = await response.json(); - const accessToken = result.data.accessToken; - window.localStorage.setItem("accessToken", accessToken); - goToFolderPage(); - } - } catch (error) { - console.error("회원가입 실패: ", error); - } -} - -export { signIn, getIsNewEmail, signUp }; diff --git a/utils/apiClient.js b/utils/apiClient.js new file mode 100644 index 000000000..bf180c0e7 --- /dev/null +++ b/utils/apiClient.js @@ -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; + } +}; diff --git a/utils/auth.js b/utils/authorize.js similarity index 62% rename from utils/auth.js rename to utils/authorize.js index 212dd504d..25e32858d 100644 --- a/utils/auth.js +++ b/utils/authorize.js @@ -6,6 +6,8 @@ import { FOLDER_PAGE_PATH, } from "/utils/constants.js"; +import { fetchClient } from "./apiClient.js"; + // 비밀번호 토글 function getPasswordVisibility(inputType) { return inputType === "password" @@ -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, @@ -87,4 +126,7 @@ export { getIsCorrectPassword, getIsFilledConfirmPassword, getIsConfirmedConfirmPassword, + signIn, + getIsNewEmail, + signUp, }; diff --git a/utils/signin.js b/utils/signin.js index f03f67aa1..568704225 100644 --- a/utils/signin.js +++ b/utils/signin.js @@ -4,7 +4,8 @@ import { getIsFilledEmail, getIsValidEmail, getIsFilledPassword, -} from "/utils/auth.js"; + signIn, +} from "/utils/authorize.js"; import { AUTH_HINT, @@ -12,8 +13,6 @@ import { INPUT_HINT_CLASSNAME, } from "/utils/constants.js"; -import { signIn } from "./api.js"; - /* 로그인 상태로 접근 시 리다이렉트 */ (function () { const accessToken = localStorage.getItem("accessToken"); @@ -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) => { diff --git a/utils/signup.js b/utils/signup.js index 758f35434..fac429e90 100644 --- a/utils/signup.js +++ b/utils/signup.js @@ -7,7 +7,9 @@ import { getIsValidPassword, getIsFilledConfirmPassword, getIsConfirmedConfirmPassword, -} from "/utils/auth.js"; + signUp, + getIsNewEmail, +} from "/utils/authorize.js"; import { AUTH_HINT, @@ -15,10 +17,6 @@ import { INPUT_HINT_CLASSNAME, } from "/utils/constants.js"; -import { signUp } from "./api.js"; - -import { getIsNewEmail } from "/utils/api.js"; - /* 로그인 상태로 접근 시 리다이렉트 */ (function () { const accessToken = localStorage.getItem("accessToken"); @@ -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) => { @@ -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({