Skip to content

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

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.py
index 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 timezone
from graphene import ObjectType
from graphene.types.mutation import MutationOptions
from graphene_django.registry import get_global_registry
from graphql.error import GraphQLError
-from graphql_jwt import ObtainJSONWebToken, Verify
+from graphql_jwt import ObtainJSONWebToken, Refresh, Verify
from graphql_jwt.exceptions import JSONWebTokenError, PermissionDenied
from ...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.py
index 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, VerifyToken
from .types.common import TaxType
class 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.graphql
index 0c3db39c3f..f31e775dc9 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -2346,7 +2346,7 @@ type Mutation {
voucherCataloguesRemove(id: ID!, input: CatalogueInput!): VoucherRemoveCatalogues
voucherTranslate(id: ID!, input: NameTranslationInput!, languageCode: LanguageCodeEnum!): VoucherTranslate
tokenCreate(email: String!, password: String!): CreateToken
- tokenRefresh(token: String!): Refresh
+ tokenRefresh(token: String!): RefreshToken
tokenVerify(token: String!): VerifyToken
checkoutAddPromoCode(checkoutId: ID!, promoCode: String!): CheckoutAddPromoCode
checkoutBillingAddressUpdate(billingAddress: AddressInput!, checkoutId: ID!): CheckoutBillingAddressUpdate
@@ -3807,7 +3807,7 @@ type ReducedRate {
rateType: TaxRateType!
}
-type Refresh {
+type RefreshToken {
token: String
payload: GenericScalar
}