generated from SverreNystad/template_python_application
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Add login view with authentication
- Loading branch information
1 parent
1aa0e2d
commit 580a5ca
Showing
1 changed file
with
31 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,40 @@ | ||
from django.shortcuts import render | ||
from django.contrib.auth import authenticate | ||
|
||
# Create your views here. | ||
|
||
from rest_framework import generics | ||
from .serializers import UserSerializer | ||
from rest_framework.decorators import api_view | ||
from rest_framework.response import Response | ||
from rest_framework import status | ||
|
||
from drf_yasg.utils import swagger_auto_schema | ||
|
||
# from drf_yasg import openapi | ||
|
||
from .serializers import UserSerializer, LoginSerializer | ||
|
||
|
||
class CreateUserView(generics.CreateAPIView): | ||
serializer_class = UserSerializer | ||
|
||
|
||
@swagger_auto_schema( | ||
method="post", | ||
request_body=LoginSerializer, | ||
operation_description="Login to the application", | ||
) | ||
@api_view(["POST"]) | ||
def LoginView(request): | ||
username = request.data.get("username") | ||
password = request.data.get("password") | ||
user = authenticate(username=username, password=password) | ||
|
||
if user is not None: | ||
# Login successful | ||
return Response({"message": "Login successful"}, status=status.HTTP_200_OK) | ||
else: | ||
# Login failed | ||
return Response( | ||
{"message": "Invalid credentials"}, status=status.HTTP_401_UNAUTHORIZED | ||
) |