Skip to content

Commit

Permalink
Add register endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kdsuneraavinash committed Sep 16, 2023
1 parent cb8e11d commit 9084b5e
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
41 changes: 41 additions & 0 deletions apps/api_auth/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from django.contrib.auth import get_user_model
from django.contrib.auth.password_validation import validate_password
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer

user_model = get_user_model()


class RegisterSerializer(ModelSerializer):
password1 = serializers.CharField(write_only=True)
password2 = serializers.CharField(write_only=True)

def validate_password1(self, value):
validate_password(value)
return value

def validate(self, attrs):
if attrs["password1"] != attrs["password2"]:
raise serializers.ValidationError("Passwords do not match")
return attrs

def save(self, **kwargs):
user = user_model.objects.create_user(
username=self.validated_data["username"],
email=self.validated_data["email"],
first_name=self.validated_data["first_name"],
last_name=self.validated_data["last_name"],
password=self.validated_data["password1"],
)
return user

class Meta:
model = user_model
fields = (
"username",
"email",
"first_name",
"last_name",
"password1",
"password2",
)
3 changes: 3 additions & 0 deletions apps/api_auth/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
from drf_spectacular.utils import extend_schema
from rest_framework_simplejwt.views import TokenVerifyView

from apps.api_auth.views import RegisterView

urlpatterns = [
path("register/", RegisterView.as_view(), name="rest_login"),
path("login/", extend_schema(auth=[])(auth_views.LoginView).as_view(), name="rest_login"),
path("logout/", auth_views.LogoutView.as_view(), name="rest_logout"),
path("user/", auth_views.UserDetailsView.as_view(), name="rest_user_details"),
Expand Down
50 changes: 50 additions & 0 deletions apps/api_auth/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from typing import Any

from dj_rest_auth.app_settings import api_settings
from dj_rest_auth.utils import jwt_encode
from dj_rest_auth.views import sensitive_post_parameters_m
from django.contrib.auth import get_user_model
from drf_spectacular.utils import extend_schema
from rest_framework import status
from rest_framework.generics import CreateAPIView
from rest_framework.permissions import AllowAny
from rest_framework.response import Response

from apps.api_auth.serializers import RegisterSerializer

user_model = get_user_model()


@extend_schema(auth=[], responses={status.HTTP_201_CREATED: api_settings.JWT_SERIALIZER})
class RegisterView(CreateAPIView):
serializer_class = RegisterSerializer
permission_classes = [AllowAny]

user: Any
access_token: str
refresh_token: str

@sensitive_post_parameters_m
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)

def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
data = {
"user": self.user,
"access": self.access_token,
"refresh": self.refresh_token,
}

return Response(
api_settings.JWT_SERIALIZER(data, context=self.get_serializer_context()).data,
status=status.HTTP_201_CREATED,
headers=headers,
)

def perform_create(self, serializer):
self.user = serializer.save()
self.access_token, self.refresh_token = jwt_encode(self.user)
Empty file removed apps/users/views.py
Empty file.

0 comments on commit 9084b5e

Please sign in to comment.