Skip to content

Commit fd08a54

Browse files
committed
refactor(mentor): api 통신 개선
1 parent 0bc5b12 commit fd08a54

File tree

5 files changed

+70
-99
lines changed

5 files changed

+70
-99
lines changed

utils/api.js

Lines changed: 0 additions & 74 deletions
This file was deleted.

utils/apiClient.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { BASE_URL } from "./constants.js";
2+
3+
export const fetchClient = async ({ url, method, body }) => {
4+
try {
5+
const response = await fetch(`${BASE_URL}/${url}`, {
6+
method,
7+
headers: {
8+
"Content-Type": "application/json",
9+
},
10+
body: JSON.stringify(body),
11+
});
12+
13+
if (response.status !== 200) {
14+
throw new Error(response.status);
15+
}
16+
17+
const data = await response.json();
18+
return data;
19+
} catch (error) {
20+
throw error;
21+
}
22+
};

utils/auth.js renamed to utils/authorize.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
FOLDER_PAGE_PATH,
77
} from "/utils/constants.js";
88

9+
import { fetchClient } from "./apiClient.js";
10+
911
// 비밀번호 토글
1012
function getPasswordVisibility(inputType) {
1113
return inputType === "password"
@@ -77,6 +79,36 @@ function getIsConfirmedConfirmPassword(
7779
}
7880
}
7981

82+
const signIn = (email, password) => {
83+
const result = fetchClient({
84+
url: "sign-in",
85+
method: "POST",
86+
body: { email, password },
87+
});
88+
const accessToken = result.data.accessToken;
89+
window.localStorage.setItem("accessToken", accessToken);
90+
goToFolderPage();
91+
};
92+
93+
const getIsNewEmail = (email) => {
94+
fetchClient({
95+
url: "check-email",
96+
method: "POST",
97+
body: { email },
98+
});
99+
};
100+
101+
const signUp = (email, password) => {
102+
const result = fetchClient({
103+
url: "sign-up",
104+
method: "POST",
105+
body: { email, password },
106+
});
107+
const accessToken = result.data.accessToken;
108+
window.localStorage.setItem("accessToken", accessToken);
109+
goToFolderPage();
110+
};
111+
80112
export {
81113
getPasswordVisibility,
82114
goToFolderPage,
@@ -87,4 +119,7 @@ export {
87119
getIsCorrectPassword,
88120
getIsFilledConfirmPassword,
89121
getIsConfirmedConfirmPassword,
122+
signIn,
123+
getIsNewEmail,
124+
signUp,
90125
};

utils/signin.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ import {
44
getIsFilledEmail,
55
getIsValidEmail,
66
getIsFilledPassword,
7-
} from "/utils/auth.js";
7+
signIn,
8+
} from "/utils/authorize.js";
89

910
import {
1011
AUTH_HINT,
1112
INPUT_STATUS,
1213
INPUT_HINT_CLASSNAME,
1314
} from "/utils/constants.js";
1415

15-
import { signIn } from "./api.js";
16-
1716
/* 로그인 상태로 접근 시 리다이렉트 */
1817
(function () {
1918
const accessToken = localStorage.getItem("accessToken");
@@ -106,10 +105,9 @@ function getIsCompletePassword(password) {
106105
return true;
107106
}
108107
}
109-
110-
async function clickSignin(email, password) {
108+
function clickSignin(email, password) {
111109
if (getIsCompleteEmail(email) && getIsCompletePassword(password))
112-
await signIn({ email, password });
110+
signIn(email, password);
113111
}
114112

115113
emailInputElement.addEventListener("focusout", (e) => {

utils/signup.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@ import {
77
getIsValidPassword,
88
getIsFilledConfirmPassword,
99
getIsConfirmedConfirmPassword,
10-
} from "/utils/auth.js";
10+
signUp,
11+
getIsNewEmail,
12+
} from "/utils/authorize.js";
1113

1214
import {
1315
AUTH_HINT,
1416
INPUT_STATUS,
1517
INPUT_HINT_CLASSNAME,
1618
} from "/utils/constants.js";
1719

18-
import { signUp } from "./api.js";
19-
20-
import { getIsNewEmail } from "/utils/api.js";
21-
2220
/* 로그인 상태로 접근 시 리다이렉트 */
2321
(function () {
2422
const accessToken = localStorage.getItem("accessToken");
@@ -105,8 +103,8 @@ function changePasswordConfirmHint(hintType) {
105103
}
106104
}
107105

108-
async function checkEmailFocusout(email) {
109-
const isNewEmail = await getIsNewEmail(email);
106+
function checkEmailFocusout(email) {
107+
const isNewEmail = getIsNewEmail(email);
110108

111109
if (!getIsFilledEmail(email)) {
112110
changeEmailHint(INPUT_STATUS.isNotFilled);
@@ -141,8 +139,8 @@ function checkPasswordConfirmFocusout(confirmPassword) {
141139
}
142140
}
143141

144-
async function getIsCompleteEmail(email) {
145-
const isNewEmail = await getIsNewEmail(email);
142+
function getIsCompleteEmail(email) {
143+
const isNewEmail = getIsNewEmail(email);
146144

147145
if (!getIsFilledEmail(email)) {
148146
changeEmailHint(INPUT_STATUS.isNotFilled);
@@ -186,13 +184,13 @@ function getIsConfirmedPassword(confirmPassword) {
186184
}
187185
}
188186

189-
async function clickSignup({ email, password, confirmPassword }) {
187+
function clickSignup({ email, password, confirmPassword }) {
190188
if (
191189
getIsCompleteEmail(email) &&
192190
getIsCompletePassword(password) &&
193191
getIsConfirmedPassword(confirmPassword)
194192
)
195-
await signUp({ email, password });
193+
signUp({ email, password });
196194
}
197195

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

210-
emailInputElement.addEventListener("focusout", (e) => {
211-
checkEmailFocusout(e.target.value);
212-
});
213-
214-
passwordInputElement.addEventListener("focusout", (e) => {
215-
checkPasswordFocusout(e.target.value);
216-
});
217-
218208
signupButtonElement.addEventListener("click", (e) => {
219209
e.preventDefault();
220210
clickSignup({

0 commit comments

Comments
 (0)