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

✨ Add token authentication to LoginAPIView and ToDoListCreateAPI. #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions src/todo_core/to_do/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,37 @@
from to_do.serializer import ToDoListSerializer


class LoginAPIView(APIView):
def post(self, request):
user_id = request.data['id']
password = request.data['password']

if not User.objects.filter(user_id=user_id).exists():
return Response(status=401)

db_user_data = User.objects.get(user_id=user_id)
if db_user_data.password != password:
return Response(status=401)

token = Token.objects.create(token=random.random())

return Response(data={'token': token})

class LogoutAPIView(APIView):
def delete(self, request):
token = request.query_params['token']
Token.objects.filter(token=token).delete()
return Response()

class ToDoListCreateAPI(APIView):
def get(self, request):
# token 검사로직 추가, 만약 통과하면 아래 로직 그대로 실행
if 'token' not in request.query_params:
return Response(status=401)
token = request.query_params['token']
if not Token.objects.filter(token=token).exists():
return Response(status=401)

todo = ToDo.objects.all()
return_data = ToDoListSerializer(todo, many=True).data
return Response(return_data)
Expand Down