Skip to content
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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions action/base_url.js
Copy link
Collaborator

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 를 만들어 거기서 상수값을 관리하는 형태는 어떨런지요

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
38 changes: 18 additions & 20 deletions action/login.js
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ugaugaugaugaugauga

실제 서버에서 반환하는 오류 메시지를 로깅하는 부분이 없구뇽!
이러면 디버깅할때 지옥이 펼쳐진답니다.

서버에서 반환하는 에러미시도 로깅하도록 구현해볼까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ugaugaugaugaugauga

로그인의 결과는 sucess값이 아닌 로그인 한 사용자에 대한 토큰과 사용자 정보일 것 같아요.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그렇군요! 그럼 클라이언트에서 localStorege를 써서 token을 저장하는 로직을 넣어야 겠내요.

console.error('Error:', error)
return { success: false, message: '서버 요청에서 에러가 발생하였습니다.' }
Expand Down
39 changes: 16 additions & 23 deletions action/register.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
export default async function register(email, password) {
const url = 'https://bootcamp-api.codeit.kr/api/sign-up'
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 register(registerOption) {
console.log(registerOption)
try {
const response = await fetch(url, options)
const responseData = await response.json()
if (responseData.error) {
const response = await fetch(`${BASE_URL}/sign-up`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(registerOption),
})
if (!response?.ok) {
return {
success: false,
message: '이미 존재하는 이메일 입니다.',
message: '요청이 올바르지 않습니다.',
}
}
if (!!responseData.data) {
localStorage.setItem('accessToken', responseData.data.accessToken)
return { success: true }
}
return { success: false, message: '서버 에러' }

const responseData = await response.json()
localStorage.setItem('accessToken', responseData.data.accessToken)
return { success: true }
} catch (error) {
console.log('Error:', error)
return { success: false, message: '서버 에러' }
Expand Down
7 changes: 0 additions & 7 deletions signin/js/get-element.js

This file was deleted.

6 changes: 0 additions & 6 deletions signin/js/handle-event/handle-email-focus-in.js

This file was deleted.

11 changes: 0 additions & 11 deletions signin/js/handle-event/handle-email-focus-out.js

This file was deleted.

8 changes: 0 additions & 8 deletions signin/js/handle-event/handle-eye-off-click.js

This file was deleted.

8 changes: 0 additions & 8 deletions signin/js/handle-event/handle-eye-on-click.js

This file was deleted.

6 changes: 0 additions & 6 deletions signin/js/handle-event/handle-password-focus-in.js

This file was deleted.

10 changes: 0 additions & 10 deletions signin/js/handle-event/handle-password-focus-out.js

This file was deleted.

38 changes: 0 additions & 38 deletions signin/js/handle-event/handle-submit.js

This file was deleted.

39 changes: 39 additions & 0 deletions signin/js/handle-submit.js
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ugaugaugaugaugauga

우리 멘토링때 이야기했던것처럼 너무 작은 단위의 행동행동별로 구분하기보단
역할과 책임을 다룬 도메인별로 로직을 나눠 처리하도록 하면 어떨까요?

handleSubmit 이라는 함수는 로그인 폼에서만 활용되죠? 그럼 이건 오히려 main.js에 속하는게 좋아보여요!
form query selector와 함꼐 위치시켜주세요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ugaugaugaugaugauga

굳이 showError와 같은 함수를 사용하지 않은 이유가 있으실까요?
만약 요구사항에서 단순 텍스트 오류만 보여줘도 되는거라면 무시해주셔도 좋습니다!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

말씀해주신 것 처럼 요구사항에 텍스트 오류만 보여줘도 되서 뺏어요 ㅎㅎ


const isLogin = await login({ email, password })
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ugaugaugaugaugauga

멘토링에서 이야기했던것처럼 어디서 에러 핸들링을 하면 좋을지 고민해봐도 좋겠어요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 참고하겠습니다!


if (!isLogin.success) {
emailErrorEl.textContent = isLogin.message
return
}

redirect('/folder')
}
59 changes: 36 additions & 23 deletions signin/js/main.js
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)
35 changes: 35 additions & 0 deletions signin/js/ui-controller.js
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'
}
11 changes: 0 additions & 11 deletions signin/js/utils/validate-email.js

This file was deleted.

6 changes: 0 additions & 6 deletions signin/js/utils/validate-password.js

This file was deleted.

17 changes: 0 additions & 17 deletions signup/js/get-elements.js

This file was deleted.

12 changes: 0 additions & 12 deletions signup/js/handle-event/handle-confirm-eye-off-click.js

This file was deleted.

Loading
Loading