Skip to content
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
3 changes: 2 additions & 1 deletion Users/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib import admin
from .models import MyUser

# Register your models here.
admin.site.register(MyUser)
14 changes: 13 additions & 1 deletion Users/models.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions Users/templates/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<title>로그인</title>
</head>
<body>
<h1>로그인</h1>
<form action="{% url 'login' %}" method="post">
{% csrf_token %}
<label for="login-name">이름:</label>
<input type="text" id="login-name" name="name" required><br><br>

<label for="login-password">비밀번호:</label>
<input type="password" id="login-password" name="password" required><br><br>

<button type="submit">로그인</button>
</form>
</body>
</html>
19 changes: 19 additions & 0 deletions Users/templates/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<title>메인 페이지</title>
</head>
<body>
<h1>메인 페이지</h1>
{% if user.is_authenticated %}
<p>로그인된 사용자: {{ user.username }}</p>
<form action="{% url 'logout' %}" method="post">
{% csrf_token %}
<button type="submit">로그아웃</button>
</form>
{% else %}
<p>로그인 안 됨</p>
<a href="{% url 'login' %}">로그인</a>
{% endif %}
</body>
</html>
77 changes: 77 additions & 0 deletions Users/templates/signup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<!-- <!DOCTYPE html>
<html>
<head>
<title>회원가입</title>
</head>
<body>
<h1>회원가입</h1>
<form action="/api/signup/" method="post">
{% csrf_token %}
<label for="signup-name">이름:</label>
<input type="text" id="signup-name" name="name" required><br><br>

<label for="signup-password">비밀번호:</label>
<input type="password" id="signup-password" name="password" required><br><br>

<label for="signup-country">나라:</label>
<select id="signup-country" name="country" required>
<option value="" disabled selected>나라 선택</option>

<option value="대한민국">Republic of Korea</option>
<option value="미국">United States of America</option>
<option value="싱가포르">Singapore</option>
<option value="영국">United Kingdom</option>
<option value="독일">Germany</option>
<option value="프랑스">France</option>
<option value="스위스">Swiss</option>
<option value="덴마크">Denmark</option>
<option value="네덜란드">Netherlands</option>
<option value="캐나다">Canada</option>
<option value="일본">Japan</option>
<option value="중국">China</option>

</select><br><br>

<button type="submit">가입하기</button>
</form>
</body>
</html> -->


<!DOCTYPE html>
<html>
<head>
<title>회원가입</title>
</head>
<body>
<h1>회원가입</h1>
<form method="post">
{% csrf_token %}
<label for="signup-name">이름:</label>
<input type="text" id="signup-name" name="name" required><br><br>

<label for="signup-password">비밀번호:</label>
<input type="password" id="signup-password" name="password" required><br><br>

<label for="signup-country">나라:</label>
<select id="signup-country" name="country" required>
<option value="" disabled selected>나라 선택</option>
<option value="대한민국">Republic of Korea</option>
<option value="미국">United States of America</option>
<option value="싱가포르">Singapore</option>
<option value="영국">United Kingdom</option>
<option value="독일">Germany</option>
<option value="프랑스">France</option>
<option value="스위스">Swiss</option>
<option value="덴마크">Denmark</option>
<option value="네덜란드">Netherlands</option>
<option value="캐나다">Canada</option>
<option value="일본">Japan</option>
<option value="중국">China</option>
</select><br><br>

<button type="submit">가입하기</button>
</form>
</body>
</html>

12 changes: 12 additions & 0 deletions Users/templates/signup_complete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>회원가입 완료</title>
</head>
<body>
<h1>회원가입이 완료되었습니다!</h1>
<p>이름: {{ name }}</p>
<p>나라: {{ country }}</p>
<p><a href="/">메인페이지로 가기</a></p>
</body>
</html>
54 changes: 53 additions & 1 deletion Users/views.py
Original file line number Diff line number Diff line change
@@ -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.'})
32 changes: 29 additions & 3 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
'Restaurants',
'Reviews',
'rest_framework',
'corsheaders',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -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': [
Expand Down Expand Up @@ -160,4 +159,31 @@
try:
from .local_settings import *
except ImportError:
pass
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'
11 changes: 9 additions & 2 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
]

1 change: 1 addition & 0 deletions server
Submodule server added at 1f1917