Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Frontend url sending on registration
Tomasz Szymański1cc5a7997ce0
diff --git a/saleor/account/emails.py b/saleor/account/emails.pyindex 118e74585e..5efa70b98b 100644--- a/saleor/account/emails.py+++ b/saleor/account/emails.py@@ -1,7 +1,6 @@from urllib.parse import urlencodefrom django.contrib.auth.tokens import default_token_generator-from django.contrib.sites.models import Sitefrom templated_email import send_templated_mailfrom ..account import events as account_events@@ -20,16 +19,15 @@ def send_user_password_reset_email_with_url(redirect_url, user):_send_password_reset_email_with_url.delay(user.email, redirect_url, user.pk, token)-def send_account_confirmation_email(user):+def send_account_confirmation_email(user, redirect_origin):"""Trigger sending an account confirmation email for the given user."""token = default_token_generator.make_token(user)- _send_account_confirmation_email.delay(user.email, token)+ _send_account_confirmation_email.delay(user.email, token, redirect_origin)@app.task-def _send_account_confirmation_email(email, token):- domain = Site.objects.get_current().domain- confirm_url = f"https://{domain}/account-confirm?email={email}&token={token}"+def _send_account_confirmation_email(email, token, redirect_origin):+ confirm_url = f"{redirect_origin}/account-confirm?email={email}&token={token}"send_kwargs, ctx = get_email_context()ctx["confirm_url"] = confirm_urlsend_templated_mail(diff --git a/saleor/graphql/account/mutations/account.py b/saleor/graphql/account/mutations/account.pyindex 5cd14156ef..fe900ca955 100644--- a/saleor/graphql/account/mutations/account.py+++ b/saleor/graphql/account/mutations/account.py@@ -29,6 +29,10 @@ from .base import (class AccountRegisterInput(graphene.InputObjectType):email = graphene.String(description="The email address of the user.", required=True)password = graphene.String(description="Password.", required=True)+ redirect_origin = graphene.String(+ description="Base of frontend URL that will be needed to create confirmation URL.",+ required=True,+ )class AccountRegister(ModelMutation):@@ -44,6 +48,16 @@ class AccountRegister(ModelMutation):error_type_class = AccountErrorerror_type_field = "account_errors"+ @classmethod+ def clean_input(cls, info, instance, data, input_cls=None):+ try:+ validate_storefront_url(data["redirect_origin"])+ except ValidationError as error:+ raise ValidationError(+ {"redirect_origin": error}, code=AccountErrorCode.INVALID+ )+ return super().clean_input(info, instance, data, input_cls=None)+@classmethoddef save(cls, info, user, cleaned_input):password = cleaned_input["password"]@@ -51,7 +65,9 @@ class AccountRegister(ModelMutation):if settings.ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL:user.is_active = Falseuser.save()- emails.send_account_confirmation_email(user)+ emails.send_account_confirmation_email(+ user, cleaned_input["redirect_origin"]+ )else:user.save()account_events.customer_account_created_event(user=user)diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 749a5ccd71..9ba52044b5 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -73,6 +73,7 @@ type AccountRegister {input AccountRegisterInput {email: String!password: String!+ redirectOrigin: String!}type AccountRequestDeletion {@@ -2272,7 +2273,7 @@ type Mutation {checkoutUpdatePrivateMetadata(id: ID!, input: MetaInput!): CheckoutUpdatePrivateMetacheckoutClearPrivateMetadata(id: ID!, input: MetaPath!): CheckoutClearPrivateMetarequestPasswordReset(email: String!, redirectUrl: String!): RequestPasswordReset- confirmAccount(email: String!, token: String!): ConfirmAccount+ confirmAccount(email: String!, redirectOrigin: String!, token: String!): ConfirmAccountsetPassword(token: String!, email: String!, password: String!): SetPasswordpasswordChange(newPassword: String!, oldPassword: String!): PasswordChangerequestEmailChange(newEmail: String!, password: String!, redirectUrl: String!): RequestEmailChangediff --git a/tests/api/test_account.py b/tests/api/test_account.pyindex 5c1326ea10..acfe84da63 100644--- a/tests/api/test_account.py+++ b/tests/api/test_account.py@@ -565,8 +565,8 @@ def test_user_with_cancelled_fulfillments(ACCOUNT_REGISTER_MUTATION = """- mutation RegisterAccount($password: String!, $email: String!) {- accountRegister(input: {password: $password, email: $email}) {+ mutation RegisterAccount($password: String!, $email: String!, $redirectOrigin: String!) {+ accountRegister(input: {password: $password, email: $email, redirectOrigin: $redirectOrigin}) {errors {fieldmessage@@ -579,11 +579,17 @@ ACCOUNT_REGISTER_MUTATION = """"""-@override_settings(ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL=True)+@override_settings(+ ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL=True, ALLOWED_CLIENT_HOSTS=["localhost"]+)@patch("saleor.account.emails._send_account_confirmation_email")def test_customer_register(send_account_confirmation_email_mock, user_api_client):email = "[email protected]"- variables = {"email": email, "password": "Password"}+ variables = {+ "email": email,+ "password": "Password",+ "redirectOrigin": "http://localhost:3000",+ }query = ACCOUNT_REGISTER_MUTATIONmutation_name = "accountRegister"response = user_api_client.post_graphql(query, variables)