๐Ÿ“’ [ TIL ] 2022.05.23_25์ผ์ฐจ # ์‚ฌ๋ฌผ์ธ์‹ ๋จธ์‹ ๋Ÿฌ๋‹(4)

๋ฌธ๋ช…์ฃผยท2022๋…„ 5์›” 23์ผ
0

[ 2022-05-23 (์›”) ์˜ค๋Š˜์˜ TIL ]

[ Today Project ]

  • ์‚ฌ๋ฌผ์ธ์‹ ๋จธ์‹ ๋Ÿฌ๋‹
    : ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ๋Š” ํŒ€ํ”„๋กœ์ ํŠธ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

[ Today Learn ]

  • ํ”„๋กœ์ ํŠธ๋ฅผ ๋นจ๋ฆฌ ๋๋‚ด๊ฒŒ๋˜์–ด ๊ฑฐ๋ถ์ด๋ฐ˜๐Ÿข ๋ณต์Šต์ง„ํ–‰(์ดํ‹€์— ๊ฑธ์ณ ์ง„ํ–‰์˜ˆ์ •)

โœ๏ธ ๋‚ด๊ฐ€ ๋ฐฐ์šด๊ฒƒ, ์–ป์€ ๊ฒƒ

  • ํ”„๋ก ํŠธ์—”๋“œ์™€ ๋ฐฑ์—”๋“œ๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋ ˆํฌํ˜•์„ฑ
    ์‹ค์ œ ์‹ค๋ฌด์—์„œ๋Š” ํ”„๋ก ํŠธ์™€ ๋ฐฑ์—”๋“œ๋ฅผ ํ•œ ๋ ˆํฌ์—์„œ ์ง„ํ–‰ํ•˜์ง€ ์•Š๊ณ  ๋ถ„๋ฆฌํ•˜์—ฌ ์ง„ํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Œ. ๋”ฐ๋ผ์„œ VSC๋ฅผ ๋‘๊ฐœ์˜ ์ฐฝ์„ ์ผœ์„œ ๋”ฐ๋กœ๋”ฐ๋กœ ์ง„ํ–‰ํ•จ.

  • postman์„ ํ™œ์šฉํ•˜์—ฌ ํ”„๋ก ํŠธ์™€ ๋ฐฑ์—”๋“œ ์—ฐ๋™
    html ๋ฐ css๋ฅผ ๋งŒ๋“ค๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ๊ณ ์นจํ•˜์—ฌ ํ™•์ธํ•  ๋ฒˆ๊ฑฐ๋กœ์›€์—†์ด Postman์„ ํ™œ์šฉํ•˜์—ฌ ์†์‰ฝ๊ฒŒ ํ™•์ธ ๊ฐ€๋Šฅ

๐Ÿ“Œ ์ด๋ ‡๊ฒŒ ๊ฒฝ๋กœ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๋˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ "CORS ์—๋Ÿฌ" ๋ฅผ ์ ์ง€์•Š๊ฒŒ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

โ“ CORS๋ž€ ?

๊ต์ฐจ ์ถœ์ฒ˜ ๋ฆฌ์†Œ์Šค ๊ณต์œ (Cross-Origin Resource Sharing)์˜ ์•ฝ์ž๋กœ, ๋ฆฌ์†Œ์Šค์˜ origin๊ณผ ์š”์ฒญํ•œ origin์ด ๋‹ค๋ฅผ ๊ฒฝ์šฐ ๋ณด์•ˆ ์ƒ์˜ ์ด์œ ๋กœ ์ž์›์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ œํ•œํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค ์ฆ‰, CORS๋ž€ ์ž์›์ด ์œ„์น˜ํ•œ ์„œ๋ฒ„์™€ ์š”์ฒญ์„ ๋ณด๋‚ธ ์›น ์„œ๋ฒ„๊ฐ€ ๊ฐ๊ธฐ ๋‹ค๋ฅธ ๊ณณ์— ์žˆ์„ ๋•Œ (Cross-Origin ์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ) ์ž์›์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์„ ์ž์›์„ ๊ฐ€์ง„ ์„œ๋ฒ„ ์ชฝ์—์„œ ์ •์˜ํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

CORS ์—๋Ÿฌ์˜ ํ•ด๊ฒฐ๋ฐฉ์•ˆ์€?

Access-Control-Allow-Origin' ํ—ค๋”๊ฐ€ ์š”์ฒญ๋ฐ›์€ ๋ฆฌ์†Œ์Šค ์ธก์—์„œ ์ •์˜๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๊ณ  ๋‚˜์™€ ์žˆ์œผ๋ฏ€๋กœ, ์ž์›์„ ๊ฐ€์ง„ ์„œ๋ฒ„ ์ธก์—์„œ Cross-Origin ์š”์ฒญ์„ ํ—ˆ์šฉํ•  ๊ฒƒ์ธ์ง€ ์ œํ•œํ•  ๊ฒƒ์ธ์ง€๊ฐ€ ๋‹ด๊ธด 'Access-Control-Allow-Origin' ํ—ค๋”๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์€ ํ•ด๋‹น ํ—ค๋”๋ฅผ ์ •์˜ํ•ด์ฃผ๋Š” ํ”Œ๋Ÿฌ๊ทธ์ธ์ด๋‚˜ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

๐Ÿงฉ ์ ์šฉ์˜ˆ์‹œ

from flask_cors import CORS
  • ๋น„๋™๊ธฐํ•จ์ˆ˜์˜ ์ •์˜ ํ™œ์šฉ๋ฐฉ๋ฒ•

โ“ ๋น„๋™๊ธฐํ•จ์ˆ˜๋ž€ ?

์ด๋ฒคํŠธ ๋ฃจํ”„๋ฅผ ํ†ตํ•ด ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ž‘๋™ํ•˜๋Š” ํ•จ์ˆ˜๋กœ, ํŠน์ • ์ฝ”๋“œ์˜ ์ฒ˜๋ฆฌ๊ฐ€ ๋๋‚˜๊ธฐ ์ „์— ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜, Promise, async await ์ด๋ ‡๊ฒŒ ํฌ๊ฒŒ 3๊ฐ€์ง€ ๋น„๋™๊ธฐ ๋ฐฉ์‹์ด ์กด์žฌํ•œ๋‹ค.

  • Ajax๊ฐ€ ์•„๋‹Œ javascript๋ฅผ ์ด์šฉํ•˜์—ฌ ํšŒ์›๊ฐ€์ž…, ๋กœ๊ทธ์ธ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ

๐Ÿงฉ ์ ์šฉ์˜ˆ์‹œ

//ํšŒ์›๊ฐ€์ž…
async function handleSignin() {

    const signupData = {
        email: document.getElementById("floatingInput").value,
        password: document.getElementById('floatingPassword').value
    }

    const response = await fetch(`${backend_base_url}/signup`, {
        method: 'POST',
        body: JSON.stringify(signupData)
    }
    )
    console.log(response)

    response_json = await response.json()
    console.log(response_json)

    if (response.status == 200) {
        window.location.replace(`${frontend_base_url}/login.html`);
    } else {
        alert(response.status)
    }
}
//๋กœ๊ทธ์ธํ•˜๊ธฐ
async function handleLogin() {
    console.log("handle login")

    const loginData = {
        email: document.getElementById("floatingInput").value,
        password: document.getElementById('floatingPassword').value
    }


    const response = await fetch(`${backend_base_url}/login`, {
        method: 'POST',
        body: JSON.stringify(loginData)
    }
    )


    console.log(response)

    response_json = await response.json()
    console.log(response_json)
    localStorage.setItem("token", response_json.token)
    if (response.status == 200) {
        window.location.replace(`${frontend_base_url}/`);
    } else {
        alert(response.status)
    }

}

๐ŸŒฑ ๋Š๋‚€ ์ 

์ผ์ฐ์ด ์‚ฌ๋ฌผ์ธ์‹ํ”„๋กœ์ ํŠธ๋ฅผ ๋๋‚ด๊ณ  ๊ทธ๋™์•ˆ ๊ฑฐ๋ถ์ด๋ฐ˜๐Ÿข(๋ณด์ถฉ๋ฐ˜?) ํŠœํ„ฐ๋‹˜์˜ ๊ฐ•์˜๋ฅผ ๋“ค์œผ๋ฉด์„œ ๋ฐฐ์› ๋˜ ๋‚ด์šฉ๊ด€๋ จํ•˜์—ฌ ๋ณต์Šต์„ ์ง„ํ–‰ํ–ˆ๋‹ค. ์—ฌํƒœ๊ป ์•ž์„œ ์ง„ํ–‰ํ–ˆ๋˜ ์ธ์Šคํƒ€ํด๋ก ์—์„œ๋Š” Ajax๋ฅผ ์ด์šฉํ•ด์„œ html์ž์ฒด๋ฅผ ๋ฐฑ์—”๋“œ์—์„œ ๋„˜๊ฒจ์ฃผ๋Š” ํ˜•์‹์œผ๋กœ ์ง„ํ–‰ํ–ˆ์—ˆ๋Š”๋ฐ, ์ด๋ฒˆ์—๋Š” Ajax๋ฅผ ์ด์šฉํ•˜์ง€์•Š๊ณ  javascript๋ฅผ ์ด์šฉํ•ด์„œ ํšŒ์›๊ฐ€์ž…,๋กœ๊ทธ์ธํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์› ๋‹ค. javascript๋งŒ์„ ์ด์šฉํ•˜๋‹ˆ๊นŒ ์–ธ์–ด๊ฐ€ ์ง๊ด€์ ์ด์ง€์•Š์•„์„œ ๋จธ๋ฆฌ์— ํ•œ๊บผ๋ฒˆ์— ๋“ค์–ด์˜ค์ง€์•Š์•˜๋‹ค. ํ•˜์ง€๋งŒ ์ต์ˆ™ํ•ด์ง€๋‹ค๋ณด๋ฉด ๋ˆˆ์— ์–ธ์–ด๊ฐ€ ์ต์–ด์„œ ๊ธฐ๋Šฅ์„ ์ž˜ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์„๊ฒƒ๊ฐ™๋‹ค.

๐ŸŽ ์ž์„ธํ•œ ์ฝ”๋“œ๋Š” Moonmoo ์— ๊ฒŒ์‹œํ–ˆ์Šต๋‹ˆ๋‹ค

profile
ํ•˜๋ฃจ ํ•œ๊ฑธ์Œ์”ฉ ๊พธ์ค€ํžˆ ๋‚˜์•„๊ฐ€๋Š” ๊ฐœ๋ฐœ์ž๐Ÿ™†โ€โ™€๏ธ https://github.com/Moonmooj

0๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด