Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add helper function to retireve user from credentials

Maciej Korycinski50626737bd65
diff --git a/saleor/graphql/account/mutations/jwt.py b/saleor/graphql/account/mutations/jwt.py
index b986709a7d..914f9dc96a 100644
--- a/saleor/graphql/account/mutations/jwt.py
+++ b/saleor/graphql/account/mutations/jwt.py
@@ -1,3 +1,5 @@
+from typing import Optional
+
import graphene
import jwt
from django.core.exceptions import ValidationError
@@ -69,10 +71,21 @@ class CreateToken(BaseMutation):
)
user = graphene.Field(User, description="A user instance.")
+ @classmethod
+ def _retrieve_user_from_credentials(cls, email, password) -> Optional[models.User]:
+ user = models.User.objects.filter(email=email).first()
+ if not user:
+ return None
+ if not user.is_active:
+ return None
+ if not user.check_password(password):
+ return None
+ return user
+
@classmethod
def get_user(cls, info, data):
- user = models.User.objects.filter(email=data["email"]).first()
- if not user or not user.is_active or not user.check_password(data["password"]):
+ user = cls._retrieve_user_from_credentials(data["email"], data["password"])
+ if not user:
raise ValidationError(
{
"email": ValidationError(