From d0894e5208072e2203eebe161d7ce0a3bd7e085b 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 | 63 +++++++++++++++++++ 2 files changed, 68 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..ddbdd1b20 --- /dev/null +++ b/apps/core/client/mutations/submit-customer-change-password.ts @@ -0,0 +1,63 @@ +import { z } from 'zod'; + +import { getSessionCustomerId } from '~/auth'; + +import { client } from '..'; +import { graphql } from '../graphql'; + +export const CustomerChangePasswordSchema = z + .object({ + currentPassword: z.string(), + newPassword: z.string(), + confirmPassword: z.string(), + }) + .required(); + +const Input = CustomerChangePasswordSchema.omit({ confirmPassword: true }); + +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 customerId = await getSessionCustomerId(); + const variables = { + input: { + currentPassword, + newPassword, + }, + }; + + const response = await client.fetch({ + document: SUBMIT_CUSTOMER_CHANGE_PASSWORD_MUTATION, + variables, + customerId, + }); + + return response.data.customer.changePassword; +};