-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb8e11d
commit 9084b5e
Showing
4 changed files
with
94 additions
and
0 deletions.
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 |
---|---|---|
@@ -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", | ||
) |
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
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 |
---|---|---|
@@ -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.