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

feat: kakao login #9

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions server/apps/wapl/migrations/0002_user_kakao_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.5 on 2023-01-31 09:13

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('wapl', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='user',
name='kakao_id',
field=models.IntegerField(default=-1),
),
]
3 changes: 2 additions & 1 deletion server/apps/wapl/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ class User(AbstractUser):
gender = None
job = None
desc = None
email = models.EmailField(null=True)
email = models.EmailField(null=True)
kakao_id = models.IntegerField(default=-1)
1 change: 1 addition & 0 deletions server/apps/wapl/templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<div>
<a href="{% url 'wapl:signup' %}">회원가입</a>
<a href="{% provider_login_url 'google' %}">구글 로그인</a>
<a href="{% url 'wapl:kakao_login' %}">카카오 로그인</a>
</div>
</div>
</div>
Expand Down
Empty file.
4 changes: 2 additions & 2 deletions server/apps/wapl/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
app_name = "wapl"

urlpatterns= [

path("main", views.main, name="main"),
path("login", views.login, name="login"),
path("logout", views.logout, name="logout"),
path("signup", views.signup, name="signup"),
path("", views.start, name="start"),
path("kakao", views.kakao_login, name="kakao_login"),
path("accounts/kakao/login/callback/", views.kakao_callback, name="kakao_callback"),
# path("plan", views.create, name="create"), # 용현님 일정 ajax
# path("plan/<int:pk>", views.comment, name="comment"), # 윤정님 일정 상세

]
52 changes: 47 additions & 5 deletions server/apps/wapl/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from django.shortcuts import render, redirect
from django.http.request import HttpRequest
from . import forms
from . import models
from django.contrib import auth


# Create your views here.
import requests
from django.http import JsonResponse

def main(request:HttpRequest,*args, **kwargs):
return render(request, "main.html")
Expand All @@ -19,7 +19,7 @@ def signup(request:HttpRequest, *args, **kwargs):
if form.is_valid():
user = form.save()
auth.login(request, user, backend='django.contrib.auth.backends.ModelBackend')
return redirect('wapl:login')
return redirect('wapl:main')
else:
return redirect('wapl:signup')
else:
Expand All @@ -36,7 +36,6 @@ def login(request:HttpRequest, *args, **kwargs):
if form.is_valid():
user = form.get_user()
auth.login(request, user, backend='django.contrib.auth.backends.ModelBackend')
print("로그인 성공~~~~~~~~~")
return redirect('wapl:main')
else:
context = {
Expand All @@ -53,3 +52,46 @@ def login(request:HttpRequest, *args, **kwargs):
def logout(request:HttpRequest, *args, **kwargs):
auth.logout(request)
return redirect('wapl:start')

def kakao_login(request, *args, **kwargs):
client_id = "9b8c21fe30f9bef16b12a8a512bf18a0"
REDIRECT_URI = "http://localhost:8000/accounts/kakao/login/callback/"
return redirect(
f"https://kauth.kakao.com/oauth/authorize?client_id={client_id}&redirect_uri={REDIRECT_URI}&response_type=code"
)

def kakao_callback(request):
try:
code = request.GET.get("code")
client_id = "9b8c21fe30f9bef16b12a8a512bf18a0"
redirect_uri = "http://localhost:8000/accounts/kakao/login/callback/"
token_request = requests.get(
f"https://kauth.kakao.com/oauth/token?grant_type=authorization_code&client_id={client_id}&redirect_uri={redirect_uri}&code={code}"
)
token_json = token_request.json()

error = token_json.get("error",None)
if error is not None :
return JsonResponse({"message": "INVALID_CODE"}, status = 400)
access_token = token_json.get("access_token")
#------get kakaotalk profile info------#
profile_request = requests.get(
"https://kapi.kakao.com/v2/user/me", headers={"Authorization" : f"Bearer {access_token}"},
)
profile_json = profile_request.json()
kakao_id = profile_json.get("id")
except KeyError:
return JsonResponse({"message" : "INVALID_TOKEN"}, status = 400)
except access_token.DoesNotExist:
return JsonResponse({"message" : "INVALID_TOKEN"}, status = 400)

if models.User.objects.filter(kakao_id = kakao_id).exists():
user = models.User.objects.get(kakao_id = kakao_id)
login(request, user)
return redirect('wapl:main')
else:
user = models.User(
kakao_id = kakao_id,
).save()
login(request, user)
return redirect('wapl:main')