Skip to content

Commit

Permalink
support login token (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
mainlyIt authored Feb 5, 2025
1 parent bd45175 commit 59a3968
Showing 1 changed file with 44 additions and 20 deletions.
64 changes: 44 additions & 20 deletions vespadb/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.authtoken.models import Token

from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi

from vespadb.permissions import IsAdminOrSelf
from vespadb.users.models import VespaUser as User
from vespadb.users.serializers import ChangePasswordSerializer, LoginSerializer, UserSerializer
from vespadb.users.serializers import (
ChangePasswordSerializer,
LoginSerializer,
UserSerializer,
)


class UserViewSet(viewsets.ModelViewSet):
Expand Down Expand Up @@ -77,26 +85,42 @@ class LoginView(APIView):
authentication_classes: Sequence[type[BaseAuthentication]] = []
permission_classes = [permissions.AllowAny]

@swagger_auto_schema(
operation_summary="User Login",
operation_description="Authenticate a user by username and password, log them in and return a bearer token.",
request_body=LoginSerializer,
responses={
200: openapi.Response(
description="Login successful",
examples={
"application/json": {
"detail": "Login successful.",
"token": "0123456789abcdef0123456789abcdef01234567"
}
},
),
400: "Bad Request",
},
)
def post(self, request: Request) -> Response:
"""
Authenticate a user based on username and password.
Args:
request (Request): The HTTP request object.
Returns
-------
Response: Status indicating the success or failure of the login attempt.
"""
serializer = LoginSerializer(data=request.data)

if serializer.is_valid():
user = serializer.validated_data
login(request, user)
return Response({"detail": "Login successful."}, status=status.HTTP_200_OK)

return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

"""
Authenticate a user based on username and password, log them in and return a bearer token.
"""
serializer = LoginSerializer(data=request.data)
if serializer.is_valid():
user = serializer.validated_data
login(request, user)
# Create or retrieve the token for the user
token, _ = Token.objects.get_or_create(user=user)
return Response(
{
"detail": "Login successful.",
"token": token.key,
},
status=status.HTTP_200_OK,
)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class LogoutView(APIView):
"""API view for user logout."""
Expand Down

0 comments on commit 59a3968

Please sign in to comment.