API
๋ง๋ค๊ธฐajax๋ฅผ ํตํด ์ค์ ๋ก ํ์๊ฐ์ ํ๋ API๋ฅผ ๋ง๋ค์ด ์ฃผ๋๋ก ํ๊ฒ ๋ค.
password
๋ ์ํธํ๋ฅผ ํ์ฌ DB์ ์ ์ฅํ๋ค.
์ํธํ์๋ ๋จ๋ฐฉํฅ/์๋ฐฉํฅ์ด ์๋๋ฐ,
์ด๋ ๋จ๋ฐฉํฅ์ ์ํธํ ์ํจ ํ ๋ค์ ์๋ ๋ชจ์ต์ผ๋ก ๋์๊ฐ ์ ์๊ณ ,
์๋ฐฉํฅ์ ์ํธํ ์ํจ ํ ๋ค์ ์๋ ๋ชจ์ต์ผ๋ก '๋ณตํธํ'๊ฐ ๊ฐ๋ฅํ๋ค.
์ด๋ password
๊ฐ์ ๊ฒฝ์ฐ๋ ๋ณตํธํ๊ฐ ๊ฐ๋ฅํ๋ฉด ์๋๊ธฐ ๋๋ฌธ์ ๋จ๋ฐฉํฅ ์ํธํ๋ฅผ ์ฌ์ฉํ๋ค !
์์์ ์ฅ๊ณ ์ ์ ๋ชจ๋ธ์์๋ ๊ธฐ๋ณธ์ ์ผ๋ก password
๊ธฐ๋ณธ ํจ์๋ฅผ ์ ๊ณตํ๋ค๊ณ ํ๋ค.
์ด ๊ธฐ๋ณธํจ์์ธ make_password
๋ฅผ ์ฌ์ฉํ์ฌ password
๋ฅผ ์ํธํ์์ผ์ค ๋ค DB์ ์ ์ฅํด์ฃผ๋๋ก ํ๊ฒ ๋ค.
user/views.py
from django.contrib.auth.hashers import make_password
from rest_framework.response import Response
from .models import User
class Join(APIView):
...
def post(self, request):
# ํ์๊ฐ์
email = request.data.get('email', None)
nickname = request.data.get('nickname', None)
name = request.data.get('name', None)
password = request.data.get('password', None)
# User DB ๋ง๋ค์ด ์ฃผ๊ธฐ
User.objects.create(email=email,
nickname=nickname,
name=name,
password=make_password(password), # password๊ฐ ์ํธํ๋์ด ์ ์ฅ
profile="default_profile.jpg")
return Response(status=200)
templates/join.html
html
...
<div class="form-floating mb-3">
<input style="width: 300px; height: 24px; margin: auto;" type="email" class="form-control" id="input-email" placeholder="name@example.com">
<label style="font-size: 13px; padding: 4px 10px; margin-left: 30px; margin-top: 2px;" for="input-email">ํด๋ํฐ ๋ฒํธ ๋๋ ์ด๋ฉ์ผ ์ฃผ์</label>
</div>
<div class="form-floating mb-3">
<input style="width: 300px; height: 24px; margin: auto;" type="text" class="form-control" id="input-name" placeholder="name@example.com">
<label style="font-size: 13px; padding: 4px 10px; margin-left: 30px; margin-top: 2px;" for="input-name">์ด๋ฆ</label>
</div>
<div class="form-floating mb-3">
<input style="width: 300px; height: 24px; margin: auto;" type="text" class="form-control" id="input-nickname" placeholder="name@example.com">
<label style="font-size: 13px; padding: 4px 10px; margin-left: 30px; margin-top: 2px;" for="input-nickname">๋๋ค์</label>
</div>
<div class="form-floating mb-3">
<input style="width: 300px; height: 24px; margin: auto;" type="text" class="form-control" id="input-password" placeholder="name@example.com">
<label style="font-size: 13px; padding: 4px 10px; margin-left: 30px; margin-top: 2px;" for="input-password">๋น๋ฐ ๋ฒํธ</label>
</div>
<button id="join-button" style="width: 300px; height: 32px; margin: auto; font-size: 14px; font-weight: bolder; margin-top: 8px;" type="button" class="btn btn-primary">๊ฐ์
</button>
...
js
$('#join-button').click(function(){ // ๊ฐ์
ํ๊ธฐ ๋ฒํผ์ ํด๋ฆญํ๋ฉด
let email = $('#input-email').val();
let nickname = $('#input-nickname').val();
let name = $('#input-name').val();
let password = $('#input-password').val();
console.log(email, nickname, name, password)
});
id
๊ฐ์ ์ค์ ํด ์ค๋ค, ์ ์ด์ฟผ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์
๋ฒํผ์ ํด๋ฆญํ์ ๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋๋ก ๋ง๋ค์ด ์ฃผ์๋ค.
์ด์ ๋ด์ ๋ฐ์ดํฐ๋ฅผ ajax
๋ฅผ ์ฌ์ฉํ์ฌ ์๋ฒ๋ฅผ ๋ณด๋ด์ฃผ๋๋ก ํ๊ฒ ๋ค.
templates/join.html
js
$.ajax({
url: "/user/join",
data: {
email : email,
nickname : nickname,
name : name,
password : password
},
method: "POST",
success: function (data) {
console.log("์ฑ๊ณต");
alert("ํ์๊ฐ์
์ฑ๊ณตํ์ต๋๋ค. ๋ก๊ทธ์ธ์ ํด์ฃผ์ธ์.");
location.replace('/user/login');
},
error: function (request, status, error) {
console.log("์๋ฌ");
},
complete: function() {
console.log("์๋ฃ");
}
})
์๋ฒ๋ฅผ ์คํํด ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ๋ฃ์ด์ฃผ๊ณ ํ์๊ฐ์ ๋ฒํผ์ ๋๋ฅด๋ฉด DB์ ์ ๋ ฅ ๋ฐ์ดํฐ๊ฐ ์ ์ฅ๋๋ ๊ฒ์ ํ์ธ ํ ์ ์๋ค.
๋จผ์ ๋ทฐ๋ฅผ ๋ง๋ค์ด์ค๋ค.
user/views.py
class Login(APIView):
...
def post(self, request):
# ๋ก๊ทธ์ธ
email = request.data.get('email', None)
password = request.data.get('password', None)
user = User.objects.filter(email=email).first() # email์ unique์ ๋ณด๋ก, ์์ผ๋ฉด ๋ฌด์กฐ๊ฑด 1๊ฐ์ด๊ธฐ ๋๋ฌธ์ fist()๋ฅผ ์ฌ์ฉํด ์ฒซ๋ฒ์งธ ๊ฐ๋ง ๊บผ๋ด์จ๋ค.
if user is None: # ํด๋น ์ด๋ฉ์ผ์ด User ๋๋น์ ์๋ ๊ฒฝ์ฐ ์๋ฌ ๋ฐํ
return Response(status=400, data=dict(message="ํ์์ ๋ณด๊ฐ ์๋ชป๋์์ต๋๋ค.")) # ์๋ฌ๋ฉ์ธ์ง๋ฅผ '์์ต๋๋ค๊ฐ' ์๋ '์๋ชป๋์๋ค๊ณ ' ํ๋ ์ด์ ๋ ํด์ปค์๊ฒ ๋์์ ์ฃผ์ง ์๊ธฐ ์ํด์
if user.check_password(password): # ํด๋น ์ด๋ฉ์ผ์ด User ๋๋น์ ์๋ ๊ฒฝ์ฐ password๋ฅผ ๋น๊ต
# todo ๋ก๊ทธ์ธ ์ฑ๊ณต. ์ธ์
or ์ฟ ํค์ ๋ฃ์
return Response(status=200) # ๋น๋ฐ๋ฒํธ๊ฐ ๋ง์ ๊ฒฝ์ฐ, ๋ก๊ทธ์ธ ์ฑ๊ณต
else: # ๋น๋ฐ๋ฒํธ๊ฐ ํ๋ฆฐ๊ฒฝ์ฐ, ์๋ฌ๋ฉ์ธ์ง ๋ฐํ
return Response(status=404, data=dict(message="ํ์์ ๋ณด๊ฐ ์๋ชป๋์์ต๋๋ค."))
templates/login.html
html
...
<div class="form-floating mb-3">
<input style="width: 300px; height: 24px; margin: auto;" type="email" class="form-control" id="input-email" placeholder="name@example.com">
<label style="font-size: 13px; padding: 4px 10px; margin-left: 30px; margin-top: 2px;" for="input-email">ํด๋ํฐ ๋ฒํธ ๋๋ ์ด๋ฉ์ผ ์ฃผ์</label>
</div>
<div class="form-floating mb-3">
<input style="width: 300px; height: 24px; margin: auto;" type="text" class="form-control" id="input-password" placeholder="name@example.com">
<label style="font-size: 13px; padding: 4px 10px; margin-left: 30px; margin-top: 2px;" for="input-password">๋น๋ฐ ๋ฒํธ</label>
</div>
<button id="login-button" style="width: 300px; height: 32px; margin: auto; font-size: 14px; font-weight: bolder; margin-top: 8px;" type="button" class="btn btn-primary">๋ก๊ทธ์ธ</button>
...
js
$('#login-button').click(function(){ // ๊ฐ์
ํ๊ธฐ ๋ฒํผ์ ํด๋ฆญํ๋ฉด
let email = $('#input-email').val();
let password = $('#input-password').val();
console.log(email, password)
});
id
๊ฐ์ ์ค์ ํด ์ค๋ค, ์ ์ด์ฟผ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ ๋ก๊ทธ์ธ ๋ฒํผ์ ํด๋ฆญํ์ ๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋๋ก ๋ง๋ค์ด ์ฃผ์๋ค.
๋ด์ ๋ฐ์ดํฐ(email,password)
๋ฅผ ajax
๋ฅผ ์ฌ์ฉํ์ฌ ์๋ฒ๋ฅผ ๋ณด๋ด์ฃผ๋๋ก ํ๊ฒ ๋ค.
templates/login.html
js
$.ajax({
url: "/user/login",
data: {
email : email,
password : password
},
method: "POST",
success: function (data) {
console.log("์ฑ๊ณต");
alert("๋ก๊ทธ์ธ ์ฑ๊ณตํ์ต๋๋ค.");
location.replace('/main'); // ๋ก๊ทธ์ธ ์ฑ๊ณต์ mainํ์ด์ง๋ก ์ด๋
},
error: function (request, status, error) {
console.log("์๋ฌ");
},
complete: function() {
console.log("์๋ฃ");
}
})
์๋ฒ๋ฅผ ์คํํด ๊ฐ์ ํ ์ด๋ฉ์ผ๊ณผ ๋น๋ฐ๋ฒํธ๋ฅผ ์๋ง๊ฒ ์ ๋ ฅํ๋ฉด ๋ก๊ทธ์ธ ๋๊ณ , main ํ์ด์ง๋ก ๋์๊ฐ๋ ๊ฒ์ ํ์ธ ํ ์ ์๋ค.
7:36:10