From cd7ef497f9561049e49a4b6df29d4133ef41fdf9 Mon Sep 17 00:00:00 2001 From: jhyns Date: Fri, 23 Jun 2023 02:09:57 +0900 Subject: [PATCH] Fix login and logout --- user/urls.py | 2 +- user/viewsets.py | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/user/urls.py b/user/urls.py index b5d7311..d090577 100644 --- a/user/urls.py +++ b/user/urls.py @@ -10,6 +10,6 @@ if settings.DEBUG: else: router = routers.SimpleRouter() -router.register("users", UserViewset) +router.register("", UserViewset) urlpatterns = router.urls diff --git a/user/viewsets.py b/user/viewsets.py index 55a0832..166cac6 100644 --- a/user/viewsets.py +++ b/user/viewsets.py @@ -22,6 +22,8 @@ class UserViewset(ActionBasedMixin, ModelViewSet): permission_classes = [IsAdminUser] permission_classes_map = { "create": [AllowAny], + "login": [AllowAny], + "logout": [IsAuthenticated], "me": [IsAuthenticated], "posts": [IsAuthenticated], } @@ -42,7 +44,7 @@ class UserViewset(ActionBasedMixin, ModelViewSet): def create(self, request, *args, **kwargs): serializer = UserCreateSerializer(data=request.data) serializer.is_valid(raise_exception=True) - user = self.perform_create(serializer) + user = serializer.save() login(request, user) return Response(serializer.data, status=201) @@ -58,8 +60,7 @@ class UserViewset(ActionBasedMixin, ModelViewSet): user = authenticate(request, username=username, password=password) if user is not None: login(request, user) - serializer = UserSerializer(user) - return Response(serializer.data) + return Response({"msg": "success"}) return Response(status=401) @action(detail=False, methods=["POST"])