Fakultas Ilmu Komputer UI

Commit 4f43495a authored by Shafiya Adzhani's avatar Shafiya Adzhani
Browse files

[REFACTOR] add check password login validator

parent e13f9fac
Pipeline #66992 passed with stages
in 1 minute and 44 seconds
from django.core.exceptions import ValidationError
from django.utils.translation import gettext as _
from django.contrib.auth import authenticate
class EmptyPasswordLoginValidator:
......@@ -26,3 +27,19 @@ class EmailRegisteredLoginValidator:
except self.account.DoesNotExist:
message = _("Email is not registered yet.")
raise ValidationError(message, code="authorization")
class CheckPasswordLoginValidator:
def __init__(self, request, email, password):
self.request = request
self.email = email
self.password = password
def validate(self):
user = authenticate(
request=self.request, email=self.email.lower(), password=self.password
)
if not user:
message = _("Password is wrong.")
raise ValidationError(message, code="authorization")
......@@ -16,11 +16,10 @@ from account.etc.LoginValidator import (
EmptyPasswordLoginValidator,
EmptyEmailLoginValidator,
EmailRegisteredLoginValidator,
CheckPasswordLoginValidator,
)
from account.models import Account
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth import authenticate
class AccountSerializer(serializers.ModelSerializer):
......@@ -62,13 +61,8 @@ class LoginSerializer(serializers.Serializer):
EmptyPasswordLoginValidator().validate(password)
EmptyEmailLoginValidator().validate(email)
EmailRegisteredLoginValidator(Account).validate(email)
user = authenticate(
CheckPasswordLoginValidator(
request=self.context.get("request"), email=email.lower(), password=password
)
if not user:
message = _("Password is wrong.")
raise serializers.ValidationError(message, code="authorization")
).validate()
return attrs
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment