-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[홍진호] week6 #273
The head ref may contain hidden characters: "part1-\uD64D\uC9C4\uD638-week6-\uD53C\uB4DC\uBCA1"
[홍진호] week6 #273
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const BASE_URL = 'https://bootcamp-api.codeit.kr/api' | ||
|
||
export default BASE_URL |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,24 @@ | ||
export default async function login(email, password) { | ||
const url = 'https://bootcamp-api.codeit.kr/api/sign-in' | ||
const data = { | ||
email, | ||
password, | ||
} | ||
|
||
const options = { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(data), | ||
} | ||
import BASE_URL from './base_url.js' | ||
|
||
export default async function login(loginOption) { | ||
try { | ||
const response = await fetch(url, options) | ||
const responseData = await response.json() | ||
if (!!responseData.data) { | ||
localStorage.setItem('accessToken', responseData.data.accessToken) | ||
return { success: true } | ||
const response = await fetch(`${BASE_URL}/sign-in`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(loginOption), | ||
}) | ||
if (!response?.ok) { | ||
return { | ||
success: false, | ||
message: '이메일 혹은 패스워드가 잘못되었습니다.', | ||
} | ||
} | ||
return { success: false, message: '이메일 혹은 패스워드가 잘못되었습니다.' } | ||
Comment on lines
+12
to
17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ugaugaugaugaugauga 실제 서버에서 반환하는 오류 메시지를 로깅하는 부분이 없구뇽! 서버에서 반환하는 에러미시도 로깅하도록 구현해볼까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 로깅도 추가해보겠습니다. |
||
const responseData = await response.json() | ||
console.log(responseData) | ||
localStorage.setItem('accessToken', responseData.data.accessToken) | ||
return { success: true } | ||
} catch (error) { | ||
Comment on lines
+18
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ugaugaugaugaugauga 로그인의 결과는 sucess값이 아닌 로그인 한 사용자에 대한 토큰과 사용자 정보일 것 같아요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그렇군요! 그럼 클라이언트에서 localStorege를 써서 token을 저장하는 로직을 넣어야 겠내요. |
||
console.error('Error:', error) | ||
return { success: false, message: '서버 요청에서 에러가 발생하였습니다.' } | ||
|
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ugaugaugaugaugauga 우리 멘토링때 이야기했던것처럼 너무 작은 단위의 행동행동별로 구분하기보단
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아하! 그렇게 하는게 더 좋내요. 알려주셔서 감사합니다! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import login from '../../action/login.js' | ||
import redirect from '../../utils/redirect-home-page.js' | ||
import { | ||
validateLoginEmail, | ||
validateLoginPassword, | ||
} from '../../utils/validate-auth.js' | ||
|
||
const emailEl = document.getElementById('signIn-email') | ||
const emailErrorEl = document.getElementById('signIn-email-error') | ||
const passwordEl = document.getElementById('signIn-password') | ||
const passwordErrorEl = document.getElementById('signIn-password-error') | ||
|
||
export async function handleSubmit(e) { | ||
e.preventDefault() | ||
const email = emailEl.value | ||
const password = passwordEl.value | ||
|
||
const isValidEmail = validateLoginEmail(email) | ||
if (!isValidEmail.success) { | ||
emailErrorEl.textContent = isValidEmail.message | ||
return | ||
} | ||
|
||
const isValidPassword = validateLoginPassword(password) | ||
|
||
if (!isValidPassword.success) { | ||
passwordErrorEl.textContent = isValidPassword.message | ||
return | ||
} | ||
Comment on lines
+18
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ugaugaugaugaugauga 굳이 showError와 같은 함수를 사용하지 않은 이유가 있으실까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 말씀해주신 것 처럼 요구사항에 텍스트 오류만 보여줘도 되서 뺏어요 ㅎㅎ |
||
|
||
const isLogin = await login({ email, password }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ugaugaugaugaugauga 멘토링에서 이야기했던것처럼 어디서 에러 핸들링을 하면 좋을지 고민해봐도 좋겠어요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 참고하겠습니다! |
||
|
||
if (!isLogin.success) { | ||
emailErrorEl.textContent = isLogin.message | ||
return | ||
} | ||
|
||
redirect('/folder') | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,47 @@ | ||
import handleEmailFocusIn from './handle-event/handle-email-focus-in.js' | ||
import handlePasswordFocusOut from './handle-event/handle-password-focus-out.js' | ||
import handlePasswordFocusIn from './handle-event/handle-password-focus-in.js' | ||
import handleSubmit from './handle-event/handle-submit.js' | ||
import handleEmailFocusOut from './handle-event/handle-email-focus-out.js' | ||
import handleEyeOffIconClick from './handle-event/handle-eye-off-click.js' | ||
import handleEyeOnIconClick from './handle-event/handle-eye-on-click.js' | ||
|
||
import { | ||
emailEl, | ||
eyeOffIconEl, | ||
eyeOnIconEl, | ||
formEl, | ||
passwordEl, | ||
} from './get-element.js' | ||
import isLoggedIn from '../../utils/is-logged-in.js' | ||
import redirectHomePage from '../../utils/redirect-home-page.js' | ||
import redirect from '../../utils/redirect-home-page.js' | ||
import { handleSubmit } from './handle-submit.js' | ||
import { | ||
hideInputError, | ||
hidePassword, | ||
showPassword, | ||
validateAndDisplayEmailError, | ||
validateAndDisplayPasswordError, | ||
} from './ui-controller.js' | ||
|
||
const checkLoggedIn = isLoggedIn() | ||
if (checkLoggedIn) { | ||
redirectHomePage() | ||
redirect('/folder') | ||
} | ||
|
||
const emailEl = document.getElementById('signIn-email') | ||
const emailErrorEl = document.getElementById('signIn-email-error') | ||
const passwordEl = document.getElementById('signIn-password') | ||
const passwordErrorEl = document.getElementById('signIn-password-error') | ||
const formEl = document.getElementById('signIn-form') | ||
const eyeOffIconEl = document.getElementById('eye-off') | ||
const eyeOnIconEl = document.getElementById('eye-on') | ||
|
||
function onDocumentReady() { | ||
emailEl.addEventListener('focusout', handleEmailFocusOut) | ||
emailEl.addEventListener('focusin', handleEmailFocusIn) | ||
passwordEl.addEventListener('focusout', handlePasswordFocusOut) | ||
passwordEl.addEventListener('focusin', handlePasswordFocusIn) | ||
emailEl.addEventListener('focusin', () => { | ||
hideInputError(emailEl, emailErrorEl) | ||
}) | ||
emailEl.addEventListener('focusout', () => { | ||
validateAndDisplayEmailError(emailEl, emailErrorEl) | ||
}) | ||
passwordEl.addEventListener('focusout', () => { | ||
validateAndDisplayPasswordError(passwordEl, passwordErrorEl) | ||
}) | ||
passwordEl.addEventListener('focusin', () => { | ||
hideInputError(passwordEl, passwordErrorEl) | ||
}) | ||
eyeOffIconEl.addEventListener('click', () => { | ||
showPassword(passwordEl, eyeOffIconEl, eyeOnIconEl) | ||
}) | ||
eyeOnIconEl.addEventListener('click', () => { | ||
hidePassword(passwordEl, eyeOffIconEl, eyeOnIconEl) | ||
}) | ||
formEl.addEventListener('submit', handleSubmit) | ||
eyeOffIconEl.addEventListener('click', handleEyeOffIconClick) | ||
eyeOnIconEl.addEventListener('click', handleEyeOnIconClick) | ||
} | ||
|
||
document.addEventListener('DOMContentLoaded', onDocumentReady) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { | ||
validateLoginEmail, | ||
validateLoginPassword, | ||
} from '../../utils/validate-auth.js' | ||
|
||
export function validateAndDisplayEmailError(emailEl, emailErrorEl) { | ||
const { success, message } = validateLoginEmail(emailEl.value) | ||
if (!success) { | ||
emailErrorEl.textContent = message | ||
return | ||
} | ||
} | ||
|
||
export function validateAndDisplayPasswordError(passwordEl, passwordErrorEl) { | ||
const { success, message } = validateLoginPassword(passwordEl.value) | ||
if (!success) { | ||
passwordErrorEl.textContent = message | ||
return | ||
} | ||
} | ||
export function hideInputError(errorElement) { | ||
errorElement.textContent = '' | ||
} | ||
|
||
export function showPassword(passwordEl, eyeOffIcon, eyeOnIcon) { | ||
passwordEl.setAttribute('type', 'text') | ||
eyeOnIcon.style.display = 'inline' | ||
eyeOffIcon.style.display = 'none' | ||
} | ||
|
||
export function hidePassword(passwordEl, eyeOffIcon, eyeOnIcon) { | ||
passwordEl.setAttribute('type', 'password') | ||
eyeOffIcon.style.display = 'inline' | ||
eyeOnIcon.style.display = 'none' | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ugaugaugaugaugauga
base_url 상수값 하나만을 위해서 스크립트 파일 하나를 새로 파는건 되게 음 투 머치인듯 해요 ㅎㅎ
오히려 상수들만을 기록하는
constant.js
를 만들어 거기서 상수값을 관리하는 형태는 어떨런지요