Skip to content

Commit f2ac6e2

Browse files
committed
1-2e done
1 parent 8323a11 commit f2ac6e2

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

curl-commands.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
curl --header "Content-Type: application/json" -X POST http://127.0.0.1:8000/api/token/obtain/ --data '{"username":"djsr","password":"djsr"}'
2-
curl --header "Content-Type: application/json" -X POST http://127.0.0.1:8000/api/user/create/user/ --data '{"email":"blue2@blue.com","username":"blue2","password":"blueblue"}'
2+
curl --header "Content-Type: application/json" -X POST http://127.0.0.1:8000/api/user/create/ --data '{"email":"ichiro@mariners.com","username":"ichiro1","password":"konnichiwa"}'
33
curl --header "Content-Type: application/json" --header "Authorization: JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTU2MTYyMTg0OSwianRpIjoiYmE3OWUxZTEwOWJkNGU3NmI1YWZhNWQ5OTg5MTE0NjgiLCJ1c2VyX2lkIjoxfQ.S7tDJaaymUUNs74Gnt6dX2prIU_E8uqCPzMtd8Le0VI","access":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTYwMjIxODU1LCJqdGkiOiI0ZjFiZDE3OTE0Zjk0MjRhOTNlZDA1YTBhMTM0N2U3YSIsInVzZXJfaWQiOjg3LCJ1c2VybmFtZSI6ImJsdWU4In0.69YNgpXhkzOklIS_nXolVuYwvN4vh7jlSeQ-oQxqxjg" -X POST http://127.0.0.1:8000/api/token/refresh/ --data '{"refresh":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTU2MTYyMTg0OSwianRpIjoiYmE3OWUxZTEwOWJkNGU3NmI1YWZhNWQ5OTg5MTE0NjgiLCJ1c2VyX2lkIjoxfQ.S7tDJaaymUUNs74Gnt6dX2prIU_E8uqCPzMtd8Le0VI"}'
44
curl --header "Content-Type: application/json" -X POST http://127.0.0.1:8000/api/token/refresh/ --data '{"refresh":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTU2MTYyMTg0OSwianRpIjoiYmE3OWUxZTEwOWJkNGU3NmI1YWZhNWQ5OTg5MTE0NjgiLCJ1c2VyX2lkIjoxfQ.S7tDJaaymUUNs74Gnt6dX2prIU_E8uqCPzMtd8Le0VI"}'

djsr/authentication/serializers.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# djsr/authentication/serializers.py
22
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
3+
from rest_framework import serializers
4+
from .models import CustomUser
35

46

57
class MyTokenObtainPairSerializer(TokenObtainPairSerializer):
@@ -11,3 +13,28 @@ def get_token(cls, user):
1113
# Add custom claims
1214
token['fav_color'] = user.fav_color
1315
return token
16+
17+
18+
class CustomUserSerializer(serializers.ModelSerializer):
19+
"""
20+
Currently unused in preference of the below.
21+
"""
22+
email = serializers.EmailField(
23+
required=True
24+
)
25+
username = serializers.CharField(required=True)
26+
password = serializers.CharField(min_length=8, write_only=True, required=True)
27+
28+
class Meta:
29+
model = CustomUser
30+
fields = ('email', 'username', 'password')
31+
extra_kwargs = {'password': {'write_only': True}}
32+
33+
def create(self, validated_data):
34+
password = validated_data.pop('password', None)
35+
instance = self.Meta.model(**validated_data) # as long as the fields are the same, we can just use this
36+
if password is not None:
37+
instance.set_password(password)
38+
instance.save()
39+
return instance
40+

djsr/authentication/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from django.urls import path
22
from rest_framework_simplejwt import views as jwt_views
3-
from .views import ObtainTokenPairWithColorView
3+
from .views import ObtainTokenPairWithColorView, CustomUserCreate
44

55
urlpatterns = [
6+
path('user/create/', CustomUserCreate.as_view(), name="create_user"),
67
path('token/obtain/', ObtainTokenPairWithColorView.as_view(), name='token_create'),
78
path('token/refresh/', jwt_views.TokenRefreshView.as_view(), name='token_refresh'),
89
]

djsr/authentication/views.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1-
from django.shortcuts import render
2-
1+
from rest_framework import status, permissions
2+
from rest_framework.response import Response
33
from rest_framework_simplejwt.views import TokenObtainPairView
4-
from .serializers import MyTokenObtainPairSerializer
4+
from rest_framework.views import APIView
5+
6+
from .serializers import MyTokenObtainPairSerializer, CustomUserSerializer
57

68

79
class ObtainTokenPairWithColorView(TokenObtainPairView):
810
serializer_class = MyTokenObtainPairSerializer
11+
12+
13+
class CustomUserCreate(APIView):
14+
permission_classes = (permissions.AllowAny,)
15+
16+
def post(self, request, format='json'):
17+
serializer = CustomUserSerializer(data=request.data)
18+
if serializer.is_valid():
19+
user = serializer.save()
20+
if user:
21+
json = serializer.data
22+
return Response(json, status=status.HTTP_201_CREATED)
23+
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

0 commit comments

Comments
 (0)