๐Ÿ“’ [ TIL ] 2022.05.09_15์ผ์ฐจ # ์ธ์Šคํƒ€ UIํด๋ก (6)

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

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

[ Today Project ]

์ธ์Šคํƒ€๊ทธ๋žจ UIํด๋ก  ๋ฐฑ์—”๋“œ๊ฐœ๋ฐœ + ํ”„๋ก ํŠธ ์ถ”๊ฐ€๊ฐœ๋ฐœ
: ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ๋Š” ํŒ€ํ”„๋กœ์ ํŠธ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

[ Today Learn ]

  • ๋กœ๊ทธ์ธํ•œ ์•„์ด๋”” ๋˜๋Š” ์‚ฌ์šฉ์ž์ด๋ฆ„์˜ ๋ฉ”์ธํŽ˜์ด์ง€์™€ ๊ฐœ์ธํŽ˜์ด์ง€์ด๋™ ๊ตฌํ˜„ํ•˜๊ธฐ
  • ๊ฐœ์ธํ”„๋กœํ•„์ด๋ฏธ์ง€ ํด๋ฆญ์‹œ ๋ฉ”๋‰ด๋ชจ๋‹ฌ html ๊ตฌํ˜„ํ•˜๊ธฐ
  • ํšŒ์›๊ฐ€์ž…๊ธฐ๋Šฅ ๋ฐ ๋กœ๊ทธ์ธ๊ธฐ๋Šฅ(ํ•จ์ˆ˜ ๋ฐ ajax์š”์ฒญ) ํ”„๋ก ํŠธ๊ฐœ๋ฐœ

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

์•„๋ž˜์™€ ๊ฐ™์ด ํšŒ์›๊ฐ€์ž…์„ ํ• ๋•Œ 5๊ฐœ์˜ ๊ฐ’์„ ๋ฐ›์•„ db์— ์ €์žฅํ–ˆ๊ณ ,
'profile_pic_real'์—๋Š” ๊ณ ์ •๋œ ๊ธฐ๋ณธ ํ”„๋กœํ•„์ด๋ฏธ์ง€ ๊ฐ’์„ ์ €์žฅํ–ˆ๋‹ค.

# ํšŒ์›๊ฐ€์ž… ๊ธฐ๋Šฅ
@app.route('/sign_up/save', methods=['POST'])
def sign_up():
    id_receive = request.form['id_give']
    name_receive = request.form['name_give']
    username_receive = request.form['username_give']
    pw_receive = request.form['pw_give']

    pw_hash = hashlib.sha256(pw_receive.encode('utf-8')).hexdigest()

    doc = {
        'id': id_receive,
        'name': name_receive,
        'username': username_receive,
        'pw': pw_hash,
        'profile_pic_real': 'profile_pics/profile_placeholder.png'
    }
    db.users.insert_one(doc)
    return jsonify({'result': 'success'})

๊ทผ๋ฐ ์—ฌ๊ธฐ์„œ ๋ฌธ์ œ๋Š” ๋กœ๊ทธ์ธํ• ๋•Œ ๋ฐ›๋Š” ๊ฐ’์€ id์™€ pw ๋‹จ ๋‘๊ฐœ๋ฟ์ด์—ˆ๊ณ , ๋กœ๊ทธ์ธํ•  ์ˆ˜ ์žˆ๋Š” id๋Š” 'id'์™€ 'username' ๋‘ ๊ฐœ ๋ชจ๋‘๋กœ ์„ค์ •ํ•˜๊ณ ์‹ถ์—ˆ๋Š”๋ฐ, python๋ฌธ๋ฒ•์„ ์–ด๋–ป๊ฒŒ ์ ์šฉ์‹œ์ผœ์•ผํ• ์ง€ ๊ณ„์† ๊ณ ๋ฏผํ•˜๋‹ค ์•„๋ž˜์™€ ๊ฐ™์ด if๋ฌธ์„ ์ ์šฉํ•ด์„œ ์„ค์ •ํ• ์ˆ˜ ์žˆ์—ˆ๋‹ค.

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

# ๋กœ๊ทธ์ธ๊ธฐ๋Šฅ
@app.route('/sign_in', methods=['POST'])
def sign_in():
    id_receive = request.form['id_give']
    pw_receive = request.form['pw_give']

    pw_hash = hashlib.sha256(pw_receive.encode('utf-8')).hexdigest()
    result = db.users.find_one({'id': id_receive, 'pw': pw_hash}) 

    if result is None:       # ์—†์œผ๋ฉด 
        result = db.users.find_one({'username': id_receive, 'pw': pw_hash})
    print(result)

    if result is not None:     # ์žˆ์œผ๋ฉด 
        payload = {
            'id': result['id'],
            'exp': datetime.utcnow() + timedelta(seconds=60 * 60 * 24)  # ๋กœ๊ทธ์ธ 24์‹œ๊ฐ„ ์œ ์ง€
        }
        token = jwt.encode(payload, SECRET_KEY, algorithm='HS256')

        return jsonify({'result': 'success', 'token': token})
    # ์ฐพ์ง€ ๋ชปํ•˜๋ฉด
    else:
        return jsonify({'result': 'fail', 'msg': '์•„์ด๋””/๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.'})

๊ทผ๋ฐ ์—ฌ๊ธฐ์„œ ๋˜๋‹ค๋ฅธ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. ๋กœ๊ทธ์ธ ํ›„์—๋Š” ๋ฉ”์ธํŽ˜์ด์ง€์˜ ํ”„๋กœํ•„์‚ฌ์ง„๊ณผ ๊ฐœ์ธํŽ˜์ด์ง€๋ฅผ ์ด๋™ํ• ๋•Œ ๋“ฑ username์„ ์ด์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์งฐ๋Š”๋ฐ, id๋กœ ๋กœ๊ทธ์ธ์„ํ•˜๋ฉด ํ•ด๋‹น username์ด๋‚˜ ํ”„๋กœํ•„ ์‚ฌ์ง„์ด ๋ณด์ด์ง€ ์•Š๊ณ , username์œผ๋กœ ๋กœ๊ทธ์ธํ–ˆ์„๋•Œ๋งŒ ํ•ด๋‹น ์œ ์ €์˜ username๊ณผ ํ”„๋กœํ•„์‚ฌ์ง„์ด ๋ณด์˜€๋‹ค.
๊ทธ๋ž˜์„œ ์•„๋ž˜์™€ ๊ฐ™์ด my_info๋ณ€์ˆ˜์™€ user_info๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ’์„ ๊ฑธ๋Ÿฌ๋‚ด์–ด jinja๋ฅผ ์ด์šฉํ•˜์—ฌ html์—์„œ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜๊ฒจ์ฃผ์—ˆ๋‹ค.

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

@app.route('/user/<username>')
def user(username):
    token_receive = request.cookies.get('mytoken')
    try:
        payload = jwt.decode(token_receive, SECRET_KEY, algorithms=['HS256'])
        my_info = db.users.find_one({"id": payload['id']}, {"_id": False}) # ๋กœ๊ทธ์ธํ•œ ์•„์ด๋””๊ฐ€ db์˜ 'id'์ด๋ž‘ ๊ฐ™์€๊ฒŒ ์žˆ๋‚˜

        status = (username == my_info['username'])  # ๋‚ด ํ”„๋กœํ•„์ด๋ฉด True, ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ”„๋กœํ•„ ํŽ˜์ด์ง€๋ฉด False  / ์ž์‹ ์˜ ์œ ์ €๋„ค์ž„ ๋™์ผํ•œ์ง€

        user_info = db.users.find_one({"username": username}, {"_id": False})


        return render_template('detail.html', user_info=user_info, status=status)
    except (jwt.ExpiredSignatureError, jwt.exceptions.DecodeError):
        return redirect(url_for("home"))

์•„๋ž˜๋Š” ํ”„๋กœํ•„์ด๋ฏธ์ง€์™€ username์„ jinja๋ฅผ ์ด์šฉํ•˜์—ฌ ์ ์šฉํ•œ html์ด๋‹ค.
href๋กœ ํ•ด๋‹น์œ ์ €์˜ ๊ฐœ์ธํŽ˜์ด์ง€๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ๊ป˜ ์ ์šฉํ–ˆ๋‹ค.

 <div class="right-body">
            <div class="feed-name" style="flex-direction: row;">
                <div class="big-profile-box">
                   <a href="/user/{{ user_info.username }}" > <img class="profile-img" style="cursor: pointer;"
                         src="{{ url_for('static', filename=user_info.profile_pic_real) }}"></a>
                </div>
                <div class="name-content">
                    <a class="feed-name-text" style="margin:20px 130px 0px 0px;"
                       href="/user/{{ user_info.username }}" >{{ user_info.username }}</a>
                </div>

[๐Ÿ‘ ์˜ค๋Š˜์˜ ๊ฒฐ๊ณผ๋ฌผ ]

  • menu modal

  • ๋กœ๊ทธ์ธ์œ ์ €์˜ ๋ฉ”์ธํŽ˜์ด์ง€

  • ๋กœ๊ทธ์ธ์œ ์ €์˜ ๊ฐœ์ธํ”„๋กœํ•„ํŽ˜์ด์ง€

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

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

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

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

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

comment-user-thumbnail
2022๋…„ 5์›” 10์ผ

๋กœ๊ทธ์ธ ๊ณผ์ •์„ ์ดํ•ดํ•˜๋Š” ๊ฑด ๋ฌผ๋ก  ์–ด๋ ต์ง€๋งŒ! ํ•œ ๋ฒˆ ์ดํ•ดํ•˜๋ฉด ๋ชจ๋“  ์„œ๋น„์Šค์—์„œ ์“ฐ์ด๋Š” ๊ณตํ†ต ๊ธฐ๋Šฅ์ด๋‹ˆ ์ถฉ๋ถ„ํžˆ ์˜ค๋žœ ๊ธฐ๊ฐ„ ํ•™์Šตํ•˜์…”๋„ ์ข‹์Šต๋‹ˆ๋‹ค!! ํ™”์ดํŒ…์ž…๋‹ˆ๋‹ค!!

1๊ฐœ์˜ ๋‹ต๊ธ€

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