Skip to content

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

Add jwt token to result of SetPassword mutation

fowczarek8b1ea6524da5
diff --git a/saleor/graphql/account/mutations/base.py b/saleor/graphql/account/mutations/base.py
index b82d169616..7009775c5b 100644
--- a/saleor/graphql/account/mutations/base.py
+++ b/saleor/graphql/account/mutations/base.py
@@ -16,9 +16,11 @@ from ...account.types import Address, AddressInput, User
from ...core.mutations import (
BaseMutation,
ClearMetaBaseMutation,
+ CreateToken,
ModelDeleteMutation,
ModelMutation,
UpdateMetaBaseMutation,
+ validation_error_to_error_type,
)
BILLING_ADDRESS_FIELD = "default_billing_address"
@@ -46,22 +48,12 @@ def can_edit_address(user, address):
)
-class SetPasswordInput(graphene.InputObjectType):
- token = graphene.String(
- description="A one-time token required to set the password.", required=True
- )
- password = graphene.String(description="Password", required=True)
-
-
-class SetPassword(BaseMutation):
+class SetPassword(CreateToken):
user = graphene.Field(User, description="An user instance with new password.")
class Arguments:
- email = graphene.String(
- required=True, description="Email of a user to set password whom."
- )
- input = SetPasswordInput(
- description="Fields required to set password.", required=True
+ token = graphene.String(
+ description="A one-time token required to set the password.", required=True
)
class Meta:
@@ -71,11 +63,20 @@ class SetPassword(BaseMutation):
)
@classmethod
- def perform_mutation(cls, _root, info, **data):
+ def mutate(cls, root, info, **data):
email = data["email"]
- token = data["input"]["token"]
- password = data["input"]["password"]
+ password = data["password"]
+ token = data["token"]
+ try:
+ cls._set_password_for_user(email, password, token)
+ except ValidationError as e:
+ errors = validation_error_to_error_type(e)
+ return cls(errors=errors)
+ return super().mutate(root, info, **data)
+
+ @classmethod
+ def _set_password_for_user(cls, email, password, token):
try:
user = models.User.objects.get(email=email)
except ObjectDoesNotExist:
@@ -86,11 +87,9 @@ class SetPassword(BaseMutation):
password_validation.validate_password(password, user)
except ValidationError as error:
raise ValidationError({"password": error.messages})
-
user.set_password(password)
user.save(update_fields=["password"])
account_events.customer_password_reset_event(user=user)
- return cls(user=user)
class RequestPasswordReset(BaseMutation):
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
index 0db1c489d1..7d2678ac71 100644
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -1344,7 +1344,7 @@ def test_staff_update_errors(staff_user, customer_user, admin_user):
SET_PASSWORD_MUTATION = """
mutation SetPassword($email: String!, $token: String!, $password: String!) {
- setPassword(email: $email, input: {token: $token, password: $password}) {
+ setPassword(email: $email, token: $token, password: $password) {
errors {
field
message
@@ -1352,6 +1352,7 @@ SET_PASSWORD_MUTATION = """
user {
id
}
+ token
}
}
"""
@@ -1366,6 +1367,7 @@ def test_set_password(user_api_client, customer_user):
content = get_graphql_content(response)
data = content["data"]["setPassword"]
assert data["user"]["id"]
+ assert data["token"]
customer_user.refresh_from_db()
assert customer_user.check_password(password)