Skip to content

Commit e7a2319

Browse files
JulienLabontemerwok
authored andcommitted
apply password validators during signup (#39)
1 parent 5f6b36d commit e7a2319

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

rest_auth_toolkit/serializers.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from django.conf import settings
22
from django.contrib.auth import authenticate, get_user_model
3+
from django.contrib.auth import password_validation
4+
from django.core import exceptions
35
from django.utils.translation import gettext as _
46

57
from rest_framework import serializers
@@ -24,6 +26,23 @@ class Meta:
2426
'password': {'style': {'input_type': 'password'}},
2527
}
2628

29+
def validate(self, data):
30+
password = data['password']
31+
32+
# Create user object without saving it to get extra checks by validators
33+
user = User(**data)
34+
35+
errors = {}
36+
try:
37+
password_validation.validate_password(password=password, user=user)
38+
except exceptions.ValidationError as e:
39+
errors['password'] = list(e.messages)
40+
41+
if errors:
42+
raise serializers.ValidationError(errors)
43+
44+
return data
45+
2746
def create(self, validated_data):
2847
return User.objects.create_user(
2948
email=validated_data['email'],

0 commit comments

Comments
 (0)