HiPhone_BE/user/views.py

27 lines
734 B
Python

from django.contrib.auth import authenticate
from django.contrib.auth import login, logout
from rest_framework.response import Response
from .serializers import UserSerializer
def login_view(request):
if request.method == "POST":
username = request.data["username"]
password = request.data["password"]
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
serializer = UserSerializer(user)
return Response(serializer.data)
else:
return Response(status=401)
else:
return Response(status=405)
def logout_view(request):
logout(request)
return Response(status=204)