From e64a22bed0e4c035a9b229ed763f13a538bcc56e Mon Sep 17 00:00:00 2001 From: Alexander Saiannyi Date: Wed, 20 Mar 2024 15:17:59 +0100 Subject: [PATCH] feat(core): add change password mutation for logged in customer --- .changeset/wicked-rocks-speak.md | 5 ++ .../submit-customer-change-password.ts | 56 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 .changeset/wicked-rocks-speak.md create mode 100644 apps/core/client/mutations/submit-customer-change-password.ts diff --git a/.changeset/wicked-rocks-speak.md b/.changeset/wicked-rocks-speak.md new file mode 100644 index 000000000..0af3d2170 --- /dev/null +++ b/.changeset/wicked-rocks-speak.md @@ -0,0 +1,5 @@ +--- +"@bigcommerce/catalyst-core": patch +--- + +add change password mutation for logged in customer diff --git a/apps/core/client/mutations/submit-customer-change-password.ts b/apps/core/client/mutations/submit-customer-change-password.ts new file mode 100644 index 000000000..e20424217 --- /dev/null +++ b/apps/core/client/mutations/submit-customer-change-password.ts @@ -0,0 +1,56 @@ +import { z } from 'zod'; + +import { client } from '..'; +import { graphql } from '../graphql'; + +export const ChangePasswordSchema = z + .object({ + currentPassword: z.string(), + newPassword: z.string(), + }) + .required(); + +type SubmitCustomerChangePassword = z.infer; + +const SUBMIT_CUSTOMER_CHANGE_PASSWORD_MUTATION = graphql(` + mutation CustomerChangePassword($input: ChangePasswordInput!) { + customer { + changePassword(input: $input) { + errors { + ... on ValidationError { + message + path + } + ... on CustomerDoesNotExistError { + message + } + ... on CustomerPasswordError { + message + } + ... on CustomerNotLoggedInError { + message + } + } + } + } + } +`); + +export const submitCustomerChangePassword = async ({ + currentPassword, + newPassword, +}: SubmitCustomerChangePassword) => { + const variables = { + input: { + currentPassword, + newPassword, + }, + }; + + const response = await client.fetch({ + document: SUBMIT_CUSTOMER_CHANGE_PASSWORD_MUTATION, + variables, + }); + + return response.data.customer.changePassword; +};