Skip to content

Commit d765a97

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

File tree

5 files changed

+72
-95
lines changed

5 files changed

+72
-95
lines changed

utils/api.js

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

utils/apiClient.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
} else {
16+
return response;
17+
}
18+
} catch (error) {
19+
throw error;
20+
}
21+
};

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

Lines changed: 42 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,43 @@ function getIsConfirmedConfirmPassword(
7779
}
7880
}
7981

82+
const signIn = async (email, password) => {
83+
const response = await fetchClient({
84+
url: "sign-in",
85+
method: "POST",
86+
body: { email, password },
87+
});
88+
const result = await response.json();
89+
const accessToken = result.data.accessToken;
90+
window.localStorage.setItem("accessToken", accessToken);
91+
goToFolderPage();
92+
};
93+
94+
const getIsNewEmail = async (email) => {
95+
try {
96+
await fetchClient({
97+
url: "check-email",
98+
method: "POST",
99+
body: { email },
100+
});
101+
return true;
102+
} catch {
103+
return false;
104+
}
105+
};
106+
107+
const signUp = async (email, password) => {
108+
const response = await fetchClient({
109+
url: "sign-up",
110+
method: "POST",
111+
body: { email, password },
112+
});
113+
const result = await response.json();
114+
const accessToken = result.data.accessToken;
115+
window.localStorage.setItem("accessToken", accessToken);
116+
goToFolderPage();
117+
};
118+
80119
export {
81120
getPasswordVisibility,
82121
goToFolderPage,
@@ -87,4 +126,7 @@ export {
87126
getIsCorrectPassword,
88127
getIsFilledConfirmPassword,
89128
getIsConfirmedConfirmPassword,
129+
signIn,
130+
getIsNewEmail,
131+
signUp,
90132
};

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: 5 additions & 15 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");
@@ -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)