forked from I-deul-of-zoo/wanted-feed-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into feat/zero/I-deul-of-zoo#8
- Loading branch information
Showing
14 changed files
with
403 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from django.contrib import admin | ||
|
||
from .models import User | ||
# Register your models here. | ||
admin.site.register(User) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
|
||
from django.urls import include, path | ||
from . import views | ||
from .views import CustomLoginView | ||
from .views import UserApprovalView, SendEmailView, CustomLoginView | ||
|
||
|
||
app_name = "auth" | ||
# base_url: v1/accounts/ | ||
|
||
urlpatterns = [ | ||
path('login/', CustomLoginView.as_view(), name='custom-login'), | ||
path('', include('dj_rest_auth.urls'), name='dj_rest_auth'), | ||
#가입승인 url | ||
path('registration/', include('dj_rest_auth.registration.urls'), name='registration'), | ||
path('code/', UserApprovalView.as_view(), name='user-approval'), | ||
path('<str:username>/', SendEmailView.as_view(), name='send-email'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,111 @@ | ||
from dj_rest_auth.views import LoginView | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
from dj_rest_auth.views import LoginView | ||
from rest_framework.generics import CreateAPIView, RetrieveAPIView | ||
from django.shortcuts import get_object_or_404 | ||
from rest_framework.permissions import AllowAny, IsAuthenticatedOrReadOnly | ||
from django.core.mail import EmailMessage | ||
from rest_framework import status | ||
from .serializers import CustomLoginSerializer # 커스텀 시리얼라이저를 가져옴 | ||
from .models import User | ||
from .serializers import UserApprovalSerializer, CustomLoginSerializer | ||
|
||
|
||
# Create your views here. | ||
#! /auth/registration/ -> 테스트용 | ||
# class UserCreateView(CreateAPIView): | ||
# serializer_class = UserCreateSerializer | ||
|
||
# def perform_create(self, serializer): | ||
# data = serializer.validated_data | ||
# username = data['username'] | ||
# email = data['email'] | ||
# password = data['password'] | ||
|
||
# user = User.objects.create_user(username=username, email=email, password=password) | ||
|
||
# user.auth_code = User.objects.create_auth_code() | ||
# # message = f'가입 승인 코드: {user.auth_code}' | ||
# user.save() | ||
|
||
|
||
# # Response 확인용 | ||
# response_data = { | ||
# 'username': username, | ||
# 'auth_code': user.auth_code, | ||
# 'from_email': email, | ||
# } | ||
|
||
# print("response_data: ", response_data) | ||
# # send_mail(subject, message, from_email) #* 이메일 발송은 생략 -> 회원생성과 동시에 이메일 발송할 경우 | ||
# return Response(response_data, status = status.HTTP_201_CREATED) | ||
|
||
#? /auth/code/ : 가입승인코드 일치 확인 | ||
class UserApprovalView(APIView): # 가입승인코드 확인 | ||
permission_classes = [AllowAny] #로그인 안된 유저도 사용할 수 있게 권한부여 | ||
serializer_class = UserApprovalSerializer | ||
def post(self, request): | ||
|
||
user = request.data.get('username') | ||
auth_code = request.data.get('auth_code') | ||
# email = request.data.get('email') # 이메일 입력도 필요할 경우 사용 | ||
try: | ||
user = User.objects.get(username=user) | ||
if user.auth_code == auth_code: | ||
user.auth_code = None # 인증된 유저는 인증코드 삭제 | ||
user.is_active = True | ||
user.save() | ||
return Response({'message': '가입승인이 완료되었습니다.'}, status=status.HTTP_200_OK) | ||
else: | ||
return Response({'message': '올바르지 않은 가입승인 코드입니다.'}, status=status.HTTP_400_BAD_REQUEST) | ||
except User.DoesNotExist: | ||
return Response({'message': '사용자를 찾을 수 없습니다.'}, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
|
||
#? /auth/<username>/ : username로 인증코드 메일전송 -> 이메일 재발송이 필요한경우 | ||
class SendEmailView(RetrieveAPIView): | ||
permission_classes = [IsAuthenticatedOrReadOnly] | ||
serializer = UserApprovalSerializer | ||
lookup_field = 'username' | ||
|
||
def get(self, request, username): | ||
# user = self.get_queryset() | ||
user = get_object_or_404(User, username=username) | ||
if user.auth_code: | ||
try: | ||
subject = "메일제목" | ||
email = user.email | ||
auth_code = user.auth_code | ||
to = [email] | ||
message = auth_code # 메일 내용 #최초 회원가입시도시 생성된 인증코드 | ||
|
||
#* EmailMessage(subject=subject, body=message, to=to).send() # 메일 보내기 | ||
|
||
# Response 확인용 | ||
response_data = { | ||
'subject': subject, | ||
'message': message, | ||
'to': to, | ||
} | ||
return Response({'메일 전송 완료':response_data}, status=status.HTTP_200_OK) | ||
except Exception as e: | ||
return Response({'message': '메일 전송 중 오류가 발생했습니다.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) | ||
else: | ||
return Response({'message': '사용자를 찾을 수 없거나 인증 코드가 존재하지 않습니다.'}, status=status.HTTP_404_NOT_FOUND) | ||
|
||
def get_queryset(self): | ||
username = self.kwargs['username'] | ||
try: | ||
user = User.objects.get(username=username) | ||
except Exception as e: | ||
return 0 | ||
|
||
return user | ||
|
||
|
||
class CustomLoginView(LoginView): | ||
serializer_class = CustomLoginSerializer # 커스텀 시리얼라이저를 사용 | ||
|
||
def post(self, request, *args, **kwargs): | ||
# 로그인 로직을 그대로 유지 | ||
return super(CustomLoginView, self).post(request, *args, **kwargs) | ||
return super(CustomLoginView, self).post(request, *args, **kwargs) | ||
|
Oops, something went wrong.