diff --git a/Users/admin.py b/Users/admin.py index 8c38f3f..6d89118 100644 --- a/Users/admin.py +++ b/Users/admin.py @@ -1,3 +1,4 @@ from django.contrib import admin +from .models import MyUser -# Register your models here. +admin.site.register(MyUser) diff --git a/Users/models.py b/Users/models.py index 71a8362..fbc58ca 100644 --- a/Users/models.py +++ b/Users/models.py @@ -1,3 +1,15 @@ from django.db import models +from django.contrib.auth.models import AbstractUser -# Create your models here. +class Country(models.Model): + name = models.CharField(max_length=100) + + def __str__(self): + return self.name + +class MyUser(AbstractUser): + name = models.CharField(max_length=100) + country = models.CharField(max_length=100) + + def __str__(self): + return self.username \ No newline at end of file diff --git a/Users/templates/login.html b/Users/templates/login.html new file mode 100644 index 0000000..c6e1923 --- /dev/null +++ b/Users/templates/login.html @@ -0,0 +1,19 @@ + + + + 로그인 + + +

로그인

+
+ {% csrf_token %} + +

+ + +

+ + +
+ + diff --git a/Users/templates/main.html b/Users/templates/main.html new file mode 100644 index 0000000..d365e37 --- /dev/null +++ b/Users/templates/main.html @@ -0,0 +1,19 @@ + + + + 메인 페이지 + + +

메인 페이지

+ {% if user.is_authenticated %} +

로그인된 사용자: {{ user.username }}

+
+ {% csrf_token %} + +
+ {% else %} +

로그인 안 됨

+ 로그인 + {% endif %} + + diff --git a/Users/templates/signup.html b/Users/templates/signup.html new file mode 100644 index 0000000..d72f7af --- /dev/null +++ b/Users/templates/signup.html @@ -0,0 +1,77 @@ + + + + + + + 회원가입 + + +

회원가입

+
+ {% csrf_token %} + +

+ + +

+ + +

+ + +
+ + + diff --git a/Users/templates/signup_complete.html b/Users/templates/signup_complete.html new file mode 100644 index 0000000..ce98a3a --- /dev/null +++ b/Users/templates/signup_complete.html @@ -0,0 +1,12 @@ + + + + 회원가입 완료 + + +

회원가입이 완료되었습니다!

+

이름: {{ name }}

+

나라: {{ country }}

+

메인페이지로 가기

+ + diff --git a/Users/views.py b/Users/views.py index 91ea44a..57dc318 100644 --- a/Users/views.py +++ b/Users/views.py @@ -1,3 +1,55 @@ +from rest_framework.views import APIView +from rest_framework.response import Response +from rest_framework import status +from django.contrib.auth.hashers import make_password +from django.contrib.auth import authenticate +from django.conf import settings +import jwt from django.shortcuts import render +from django.contrib.auth import authenticate, login, logout -# Create your views here. +from .models import MyUser, Country +from django.contrib.sessions.backends.db import SessionStore + +class CountriesListView(APIView): + def get(self, request): + countries = Country.objects.all().values_list('name', flat=True) + return Response(countries) + +class SignupView(APIView): + def get(self, request): + return render(request, 'signup.html') + + def post(self, request): + name = request.data.get('name') + password = request.data.get('password') + country = request.data.get('country') + + hashed_password = make_password(password) + user = MyUser.objects.create(name=name, password=hashed_password, country=country) + + payload = {'user_id': user.id, 'name': user.name, 'country': user.country} # type: ignore + token = jwt.encode(payload, settings.SECRET_KEY, algorithm='HS256') + + context = {'name': name, 'country': country} + return render(request, 'signup_complete.html', context) + +class LoginView(APIView): + def get(self, request): + return render(request, 'login.html') + + def post(self, request): + name = request.data.get('name') + password = request.data.get('password') + + user = authenticate(request, username=name, password=password) + if user: + login(request, user) + return Response({'message': 'Logged in.'}) + else: + return Response({'message': 'Login failed.'}, status=status.HTTP_401_UNAUTHORIZED) + +class LogoutView(APIView): + def post(self, request): + logout(request) + return Response({'message': 'Logged out.'}) \ No newline at end of file diff --git a/config/settings.py b/config/settings.py index d014560..9d7c53e 100644 --- a/config/settings.py +++ b/config/settings.py @@ -47,7 +47,6 @@ 'Restaurants', 'Reviews', 'rest_framework', - 'corsheaders', ] MIDDLEWARE = [ @@ -78,7 +77,7 @@ TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], + 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ @@ -160,4 +159,31 @@ try: from .local_settings import * except ImportError: - pass \ No newline at end of file + pass + +REST_FRAMEWORK = { + 'DEFAULT_AUTHENTICATION_CLASSES': ( + 'rest_framework_simplejwt.authentication.JWTAuthentication', + ), +} + +from datetime import timedelta +from django.conf import settings + +JWT_AUTH = { + 'JWT_SECRET_KEY': settings.SECRET_KEY, + 'JWT_ALGORITHM': 'HS256', + 'JWT_ALLOW_REFRESH': True, + 'JWT_EXPIRATION_DELTA': timedelta(seconds=3600), # 토큰 만료 시간 (예: 1시간) + 'JWT_REFRESH_EXPIRATION_DELTA': timedelta(days=7), # 리프레시 토큰 만료 시간 (예: 7일) +} + +SIMPLE_JWT = { + 'ACCESS_TOKEN_LIFETIME': timedelta(minutes=60), # 토큰 만료 시간 (예: 1시간) + 'SLIDING_TOKEN_REFRESH_LIFETIME': timedelta(days=7), # 리프레시 토큰 만료 시간 (예: 7일) + 'SLIDING_TOKEN_LIFETIME': timedelta(days=30), # 슬라이딩 토큰 만료 시간 (예: 30일) + 'SLIDING_TOKEN_REFRESH_LIFETIME_ALLOW_RENEWAL': True, + 'SLIDING_TOKEN_REFRESH_LIFETIME_RENEWAL_DELTA': timedelta(days=1), +} + +AUTH_USER_MODEL = 'Users.MyUser' \ No newline at end of file diff --git a/config/urls.py b/config/urls.py index ee45a6b..d124a77 100644 --- a/config/urls.py +++ b/config/urls.py @@ -18,8 +18,15 @@ from django.urls import path from django.conf import settings from django.conf.urls.static import static +from Users import views +from Users.views import SignupView, LoginView, LogoutView, CountriesListView + urlpatterns = [ path('admin/', admin.site.urls), - ] -+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + path('signup/', SignupView.as_view(), name='signup'), + path('api/signup/', SignupView.as_view(), name='api-signup'), + path('login/', LoginView.as_view(), name='login'), + path('logout/', LogoutView.as_view(), name='logout'), + path('countries/', CountriesListView.as_view(), name='countries-list'), +] diff --git a/server b/server new file mode 160000 index 0000000..1f19174 --- /dev/null +++ b/server @@ -0,0 +1 @@ +Subproject commit 1f191741cb9870c954d7ea25b13dc3b66aa7d571