Skip to content

Commit 17eff4c

Browse files
author
Juan Benitez
committed
feat: add authentication using json web token
1 parent a604730 commit 17eff4c

File tree

17 files changed

+83
-78
lines changed

17 files changed

+83
-78
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,6 @@ venv.bak/
106106

107107
# mypy
108108
.mypy_cache/
109+
110+
# Database
111+
db.sqlite3

api_crud/settings.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
REST_FRAMEWORK = {
3131
'DEFAULT_AUTHENTICATION_CLASSES': (
32-
'rest_framework.authentication.TokenAuthentication',
32+
'rest_framework_simplejwt.authentication.JWTAuthentication',
3333
)
3434
}
3535

@@ -44,13 +44,8 @@
4444
'django.contrib.messages',
4545
'django.contrib.staticfiles',
4646
'rest_framework',
47+
'authentication',
4748
'movies',
48-
'rest_framework.authtoken',
49-
'rest_auth',
50-
'django.contrib.sites',
51-
'allauth',
52-
'allauth.account',
53-
'rest_auth.registration',
5449
]
5550

5651
SITE_ID = 1

api_crud/urls.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11

22
from django.contrib import admin
3-
from django.conf.urls import include, url
4-
from .views import RegisterView, CustomLoginView
5-
3+
from django.urls import include, path
64

75
# urls
86
urlpatterns = [
9-
url(r'^', include('movies.urls')),
10-
url(r'^rest-auth/login/', CustomLoginView.as_view()),
11-
url(r'^rest-auth/registration/', RegisterView.as_view()),
12-
url(r'^rest-auth/', include('rest_auth.urls')),
13-
url(r'^admin/', admin.site.urls),
7+
path('api/movies/', include('movies.urls')),
8+
path('api/auth/', include('authentication.urls')),
9+
path('admin/', admin.site.urls),
1410
]

api_crud/views.py

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +0,0 @@
1-
from django.utils.decorators import method_decorator
2-
from django.views.decorators.debug import sensitive_post_parameters
3-
from rest_framework.response import Response
4-
from rest_framework.generics import CreateAPIView
5-
from rest_framework import status
6-
from allauth.account.utils import complete_signup
7-
from allauth.account import app_settings as allauth_settings
8-
from rest_auth.models import TokenModel
9-
from rest_auth.views import LoginView
10-
from rest_auth.registration.app_settings import RegisterSerializer, register_permission_classes
11-
from django.contrib.auth.models import User
12-
13-
sensitive_post_parameters_m = method_decorator(
14-
sensitive_post_parameters('password1', 'password2')
15-
)
16-
17-
18-
class RegisterView(CreateAPIView):
19-
serializer_class = RegisterSerializer
20-
permission_classes = register_permission_classes()
21-
token_model = TokenModel
22-
23-
@sensitive_post_parameters_m
24-
def dispatch(self, *args, **kwargs):
25-
return super(RegisterView, self).dispatch(*args, **kwargs)
26-
27-
def create(self, request, *args, **kwargs):
28-
serializer = self.get_serializer(data=request.data)
29-
serializer.is_valid(raise_exception=True)
30-
user = self.perform_create(serializer)
31-
headers = self.get_success_headers(serializer.data)
32-
content = {
33-
"details": "Registered"
34-
}
35-
return Response(content,
36-
status=status.HTTP_201_CREATED,
37-
headers=headers)
38-
39-
def perform_create(self, serializer):
40-
user = serializer.save(self.request)
41-
42-
complete_signup(self.request._request, user, None, None)
43-
return user
44-
45-
46-
class CustomLoginView(LoginView):
47-
48-
def get_response(self):
49-
orginal_response = super().get_response()
50-
51-
custom_response = {"user": {
52-
"username": self.user.username,
53-
"email": self.user.email
54-
}}
55-
56-
orginal_response.data.update(custom_response)
57-
return orginal_response

authentication/__init__.py

Whitespace-only changes.

authentication/admin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

authentication/apps.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class AuthConfig(AppConfig):
5+
name = 'authentication'

authentication/migrations/__init__.py

Whitespace-only changes.

authentication/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.db import models
2+
3+
# Create your models here.

authentication/serializers.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from rest_framework import serializers
2+
from django.contrib.auth.models import User
3+
from rest_framework.validators import UniqueValidator
4+
from django.contrib.auth.password_validation import validate_password
5+
6+
7+
class RegisterSerializer(serializers.ModelSerializer):
8+
email = serializers.EmailField(
9+
required=True,
10+
validators=[UniqueValidator(queryset=User.objects.all())]
11+
)
12+
13+
password = serializers.CharField(write_only=True, required=True, validators=[validate_password])
14+
password2 = serializers.CharField(write_only=True, required=True)
15+
16+
class Meta:
17+
model = User
18+
fields = ('username', 'password', 'password2', 'email', 'first_name', 'last_name')
19+
20+
def validate(self, attrs):
21+
if attrs['password'] != attrs['password2']:
22+
raise serializers.ValidationError({"password": "Password fields didn't match."})
23+
24+
return attrs
25+
26+
def create(self, validated_data):
27+
del validated_data['password2']
28+
user = User.objects.create(**validated_data)
29+
30+
user.set_password(validated_data['password'])
31+
user.save()
32+
33+
return user

0 commit comments

Comments
 (0)