Skip to content

Commit

Permalink
apply password validators during signup (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienLabonte authored and merwok committed Oct 16, 2018
1 parent 5f6b36d commit e7a2319
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions rest_auth_toolkit/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.conf import settings
from django.contrib.auth import authenticate, get_user_model
from django.contrib.auth import password_validation
from django.core import exceptions
from django.utils.translation import gettext as _

from rest_framework import serializers
Expand All @@ -24,6 +26,23 @@ class Meta:
'password': {'style': {'input_type': 'password'}},
}

def validate(self, data):
password = data['password']

# Create user object without saving it to get extra checks by validators
user = User(**data)

errors = {}
try:
password_validation.validate_password(password=password, user=user)
except exceptions.ValidationError as e:
errors['password'] = list(e.messages)

if errors:
raise serializers.ValidationError(errors)

return data

def create(self, validated_data):
return User.objects.create_user(
email=validated_data['email'],
Expand Down

0 comments on commit e7a2319

Please sign in to comment.