change_password - pai-plznw4me/django-initializer GitHub Wiki

๋น„๋ฐ€ ๋ฒˆํ˜ธ ๋ณ€๊ฒฝ

  • ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ์€ ํฌ๊ฒŒ 3๊ฐ€์ง€ ์ธ์ฆ ์ ˆ์ฐจ๋ฅผ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค.
    • step 1. ์ƒˆ๋กœ์šด ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ํ™•์ธ์นธ์— ์ž…๋ ฅ๋œ ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ๊ฐ™์€์ง€ ๊ฒ€ํ† 
    • step 2. ํ™”๋ฉด์—์„œ ์ž…๋ ฅํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๊ธฐ์กด์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ๋‹ค๋ฅธ์ง€ ํ™•์ธ
    • step 3. ์ƒˆ๋กญ๊ฒŒ ์ž…๋ ฅํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๊ธฐ์กด ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ๊ฐ™์€์ง€ ํ™•์ธ
123
  • ์ƒˆ๋กœ์šด ๋น„๋ฐ€ ๋ฒˆํ˜ธ ๋ฐ ์ƒˆ๋กœ์šด ๋น„๋ฐ€ ๋ฒˆํ˜ธ ํ™•์ธ ์ด ๋งค์นญ ๋˜๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ

1. ๋น„๋ฐ€ ๋ฒˆํ˜ธ ๋ณ€๊ฒฝ HTML page ์ž‘์„ฑ

  • ์•„๋ž˜ html ์—์„œ๋Š” ์ƒˆ๋กœ์šด ๋น„๋ฐ€๋ฒˆํ˜ธ, ์ƒˆ๋กœ์šด ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ, ์ด์ „ ๋น„๋ฐ€๋ฒˆํ˜ธ ์„ ์ž…๋ ฅํ•˜๋Š” form ์ •๋ณด๋ฅผ rendering ํ•ฉ๋‹ˆ๋‹ค.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
change password
<form class="mt-4" action="{% url 'accounts:change_password' %}" method="post" id="change_password_form">
    <div class="row">
        {# new password #}
        <div class="col-lg-12">
            <div class="mb-3">
                <label class="form-label">์ƒˆ๋กœ์šด ๋น„๋ฐ€๋ฒˆํ˜ธ :</label>
                <input type="password" class="form-control" placeholder="์ƒˆ๋กœ์šด ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”"
                       required="" id="new_password" name="new_password">
            </div>
        </div>

        {# confirm new password #}
        <div class="col-lg-12">
            <div class="mb-3">
                <label class="form-label">์ƒˆ๋กœ์šด ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ:</label>
                <input type="password" class="form-control" placeholder="์ƒˆ๋กœ์šด ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋‹ค์‹œ ์ž…๋ ฅํ•˜์„ธ์š”."
                       required="" id="confirm_new_password" name="confirm_new_password">
                <span id='message'></span>
            </div>
        </div>

        {# old password #}
        <div class="col-lg-12">
            <div class="mb-3">
                <label class="form-label">์ด์ „ ๋น„๋ฐ€๋ฒˆํ˜ธ:</label>
                <input type="password" class="form-control" placeholder="์ด์ „ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”." required=""
                       name="old_password"
                       id="old_password">
            </div>
        </div>

        <div class="col-lg-12 mt-2 mb-0">
            <div class="btn btn-primary" onclick="change_password()">๋ณ€๊ฒฝํ•˜๊ธฐ</div>
        </div>
    </div>
</form>
</body>
</html>

2. ์ƒˆ๋กœ์šด ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ ํ™•์ธํ•˜๊ธฐ

  • step 1 ์„ client side ์—์„œ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
  • ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ์‹œ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋งค์นญ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
function confirm_password() {
    $('#new_password, #confirm_new_password').on('keyup', function () {
        if ($('#new_password').val() == $('#confirm_new_password').val()) {
            $('#message').html('Matching').css('color', 'green');
        } else
            $('#message').html('Not Matching').css('color', 'red');
    });
}

confirm_password()

3. ์„œ๋ฒ„์— ๋น„๋ฐ€๋ฒˆํ˜ธ ์ „์†กํ•˜๊ธฐ

  • ์ƒˆ๋กœ์šด ๋น„๋ฐ€๋ฒˆํ˜ธ, ์ƒˆ๋กœ์šด ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ, ์ด์ „ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ •๋ณด๋ฅผ ์„œ๋ฒ„์— ์ „์†กํ•ฉ๋‹ˆ๋‹ค.
  • ajax ๋กœ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.
  • ๋น„๋ฐ€ ๋ฒˆํ˜ธ ๋ณ€๊ฒฝ ์„ฑ๊ณต ์‹œ
    • ๊ธฐ์กด ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ ํผ์„ reset ํ•ฉ๋‹ˆ๋‹ค.
    • ์„ฑ๊ณต ์•Œ๋ฆผ ์ฐฝ์„ ๋„์›๋‹ˆ๋‹ค.
  • ๋น„๋ฐ€ ๋ฒˆํ˜ธ ๋ณ€๊ฒฝ ์‹คํŒจ ์‹œ
    • ๊ธฐ์กด ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ ํผ์— ์žˆ๋˜ ์ •๋ณด๋Š” ๊ทธ๋Œ€๋กœ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์‹คํŒจ ์•Œ๋ฆผ ์ฐฝ์„ ๋„์›๋‹ˆ๋‹ค.
function change_password() {
    let base_url = 'http://localhost:8000'
    let url = `${base_url}/change_password/`
    let form = $('#change_password_form')
    var form_array = form.serialize()

    console.log(form)
    $.ajax({
        url: url,
        data: form_array,
        method: "POST",
        dataType: 'json'
    }).done(function (response) {
        if (response['error'] === true) { //๋น„๋ฐ€ ๋ฒˆํ˜ธ ๋ณ€๊ฒฝ ์‹คํŒจ ์‹œ
            alert(response['msg'])
        } else if (response['error'] === false) { //๋น„๋ฐ€ ๋ฒˆํ˜ธ ๋ณ€๊ฒฝ ์„ฑ๊ณต ์‹œ
            alert(response['msg'])
            // password form ์˜ ๋ชจ๋“  ์ •๋ณด๋ฅผ ์ง€์›๋‹ˆ๋‹ค.
            form[0].reset();
            $('#message').text('')
        }
    }).fail(function () {
        alert("์š”์ฒญ ์‹คํŒจ");
    }).always(function () {
        alert("์š”์ฒญ ์™„๋ฃŒ");
    });
}

4. URLConf ๋“ฑ๋ก

path('change_password/', change_password, name='change_password'),

5. Server side ์ž‘์—…

  • step 1, step 2, step 3 ์ธ์ฆ ์ ˆ์ฐจ๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  • step 1 ์€ ์„œ๋ฒ„์—์„œ ๋‹ค์‹œ ํ•œ๋ฒˆ ๊ฒ€์ฆํ•ฉ๋‹ˆ๋‹ค.
  • 3๋‹จ๊ณ„๊ฐ€ ๋ชจ๋‘ ์ž‘๋™ํ•˜๋ฉด ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
from django.contrib.auth.hashers import check_password
from django.http import JsonResponse
def change_password(request):
    """
    Description:
        ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ฒ€์ฆ ์ ˆ์ฐจ๋ฅผ ๊ฑธ์ฒ˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
        ๋น„๋ฐ€ ๋ฒˆํ˜ธ ๊ฒ€์ฆ์€ 3๋‹จ๊ณ„๋กœ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
            step 1. ์ƒˆ๋กœ์šด ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ํ™•์ธ์นธ์— ์ž…๋ ฅ๋œ ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ๊ฐ™์€์ง€ ๊ฒ€ํ† 
            step 2. ํ™”๋ฉด์—์„œ ์ž…๋ ฅํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๊ธฐ์กด์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ๋‹ค๋ฅธ์ง€ ํ™•์ธ
            step 3. ์ƒˆ๋กญ๊ฒŒ ์ž…๋ ฅํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๊ธฐ์กด ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ๊ฐ™์€์ง€ ํ™•์ธ

    :param  request:
    :json return:

    """
    user = request.user
    new_password = request.POST.get('new_password')
    confirm_new_password = request.POST.get('confirm_new_password')
    old_password = request.POST.get('old_password')

    # step 1. ์ƒˆ๋กœ์šด ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ํ™•์ธ์นธ์— ์ž…๋ ฅ๋œ ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ๊ฐ™์€์ง€ ๊ฒ€ํ† , ๊ฐ™์œผ๋ฉด True
    if not confirm_new_password == new_password:
        data = {'msg': '์ƒˆ๋กœ์šด ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๊ธฐ์กด์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.', 'error': True}

    # step 2. ํ™”๋ฉด์—์„œ ์ž…๋ ฅํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๊ธฐ์กด์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ๋‹ค๋ฅธ์ง€ ํ™•์ธ.
    elif not check_password(old_password, user.password):
        data = {'msg': '๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ํ‹€๋ฆฝ๋‹ˆ๋‹ค. ์˜ฌ๋ฐ”๋ฅธ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.', 'error': True}

    # step 3. ์ƒˆ๋กญ๊ฒŒ ์ž…๋ ฅํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๊ธฐ์กด ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ๊ฐ™์€์ง€ ํ™•์ธ, ๊ฐ™์œผ๋ฉด False
    # ๋งŒ์•ฝ ๊ฐ™์ง€ ์•Š์œผ๋ฉด ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
    elif check_password(new_password, user.password):
        data = {'msg': '์ƒˆ๋กญ๊ฒŒ ์ž…๋ ฅํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๊ธฐ์กด ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. ', 'error': True}
    else:
        # ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝํ•˜๊ธฐ
        user.set_password(new_password)
        user.save()
        data = {'msg': '๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์•ˆ์ „ํ•˜๊ฒŒ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค', 'error': False}
    return JsonResponse(data)
โš ๏ธ **GitHub.com Fallback** โš ๏ธ