Fix login and logout

This commit is contained in:
jhyns 2023-06-23 02:09:57 +09:00
parent 59b42515f2
commit cd7ef497f9
2 changed files with 5 additions and 4 deletions

View File

@ -10,6 +10,6 @@ if settings.DEBUG:
else: else:
router = routers.SimpleRouter() router = routers.SimpleRouter()
router.register("users", UserViewset) router.register("", UserViewset)
urlpatterns = router.urls urlpatterns = router.urls

View File

@ -22,6 +22,8 @@ class UserViewset(ActionBasedMixin, ModelViewSet):
permission_classes = [IsAdminUser] permission_classes = [IsAdminUser]
permission_classes_map = { permission_classes_map = {
"create": [AllowAny], "create": [AllowAny],
"login": [AllowAny],
"logout": [IsAuthenticated],
"me": [IsAuthenticated], "me": [IsAuthenticated],
"posts": [IsAuthenticated], "posts": [IsAuthenticated],
} }
@ -42,7 +44,7 @@ class UserViewset(ActionBasedMixin, ModelViewSet):
def create(self, request, *args, **kwargs): def create(self, request, *args, **kwargs):
serializer = UserCreateSerializer(data=request.data) serializer = UserCreateSerializer(data=request.data)
serializer.is_valid(raise_exception=True) serializer.is_valid(raise_exception=True)
user = self.perform_create(serializer) user = serializer.save()
login(request, user) login(request, user)
return Response(serializer.data, status=201) return Response(serializer.data, status=201)
@ -58,8 +60,7 @@ class UserViewset(ActionBasedMixin, ModelViewSet):
user = authenticate(request, username=username, password=password) user = authenticate(request, username=username, password=password)
if user is not None: if user is not None:
login(request, user) login(request, user)
serializer = UserSerializer(user) return Response({"msg": "success"})
return Response(serializer.data)
return Response(status=401) return Response(status=401)
@action(detail=False, methods=["POST"]) @action(detail=False, methods=["POST"])