Files
ihrm/horilla_api/api_views/auth/views.py
2024-10-10 15:45:44 +05:30

27 lines
1.1 KiB
Python

from django.contrib.auth import authenticate
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework_simplejwt.tokens import RefreshToken
from ...api_serializers.auth.serializers import GetEmployeeSerializer
class LoginAPIView(APIView):
def post(self, request):
if "username" and "password" in request.data.keys():
username = request.data.get("username")
password = request.data.get("password")
user = authenticate(username=username, password=password)
if user:
refresh = RefreshToken.for_user(user)
employee = user.employee_get
result = {
"employee": GetEmployeeSerializer(employee).data,
"access": str(refresh.access_token),
}
return Response(result, status=200)
else:
return Response({"error": "Invalid credentials"}, status=401)
else:
return Response({"error": "Please provide Username and Password"})