Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Update user's last_login field in create_token, refresh_token mutations
Krzysztof Świątek4552104282be
diff --git a/saleor/graphql/core/mutations.py b/saleor/graphql/core/mutations.pyindex 83de2b4938..059e4045b9 100644--- a/saleor/graphql/core/mutations.py+++ b/saleor/graphql/core/mutations.py@@ -9,11 +9,12 @@ from django.core.exceptions import (ValidationError,)from django.db.models.fields.files import FileField+from django.utils import timezonefrom graphene import ObjectTypefrom graphene.types.mutation import MutationOptionsfrom graphene_django.registry import get_global_registryfrom graphql.error import GraphQLError-from graphql_jwt import ObtainJSONWebToken, Verify+from graphql_jwt import ObtainJSONWebToken, Refresh, Verifyfrom graphql_jwt.exceptions import JSONWebTokenError, PermissionDeniedfrom ...account import models@@ -626,6 +627,9 @@ class CreateToken(ObtainJSONWebToken):errors = validation_error_to_error_type(e)return cls.handle_typed_errors(errors)else:+ user = result.user+ user.last_login = timezone.now()+ user.save(update_fields=["last_login"])return result@classmethod@@ -641,6 +645,21 @@ class CreateToken(ObtainJSONWebToken):return cls(user=info.context.user, errors=[], account_errors=[])+class RefreshToken(Refresh):+ """Mutation that refresh user token.++ It overrides the default graphql_jwt.Refresh to update user's last_login field.+ """++ @classmethod+ def mutate(cls, root, info, **kwargs):+ result = super().mutate(root, info, **kwargs)+ user = graphene.Node.get_node_from_global_id(info, result.payload["user_id"])+ user.last_login = timezone.now()+ user.save(update_fields=["last_login"])+ return result++class VerifyToken(Verify):"""Mutation that confirms if token is valid and also returns user data."""diff --git a/saleor/graphql/core/schema.py b/saleor/graphql/core/schema.pyindex 50cbbb481e..3d41c6388f 100644--- a/saleor/graphql/core/schema.py+++ b/saleor/graphql/core/schema.py@@ -1,13 +1,12 @@import graphene-import graphql_jwt-from .mutations import CreateToken, VerifyToken+from .mutations import CreateToken, RefreshToken, VerifyTokenfrom .types.common import TaxTypeclass CoreMutations(graphene.ObjectType):token_create = CreateToken.Field()- token_refresh = graphql_jwt.Refresh.Field()+ token_refresh = RefreshToken.Field()token_verify = VerifyToken.Field()diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 0c3db39c3f..f31e775dc9 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -2346,7 +2346,7 @@ type Mutation {voucherCataloguesRemove(id: ID!, input: CatalogueInput!): VoucherRemoveCataloguesvoucherTranslate(id: ID!, input: NameTranslationInput!, languageCode: LanguageCodeEnum!): VoucherTranslatetokenCreate(email: String!, password: String!): CreateToken- tokenRefresh(token: String!): Refresh+ tokenRefresh(token: String!): RefreshTokentokenVerify(token: String!): VerifyTokencheckoutAddPromoCode(checkoutId: ID!, promoCode: String!): CheckoutAddPromoCodecheckoutBillingAddressUpdate(billingAddress: AddressInput!, checkoutId: ID!): CheckoutBillingAddressUpdate@@ -3807,7 +3807,7 @@ type ReducedRate {rateType: TaxRateType!}-type Refresh {+type RefreshToken {token: Stringpayload: GenericScalar}