diff --git a/apps/core/client/mutations/submit-change-password.ts b/apps/core/client/mutations/submit-change-password.ts new file mode 100644 index 000000000..9906f2398 --- /dev/null +++ b/apps/core/client/mutations/submit-change-password.ts @@ -0,0 +1,54 @@ +import { z } from 'zod'; + +import { client } from '..'; +import { graphql } from '../generated'; + +export const ChangePasswordSchema = z.object({ + newPassword: z.string(), + confirmPassword: z.string(), +}); + +interface SubmitChangePassword { + newPassword: z.infer['newPassword']; + token: string; + customerEntityId: number; +} + +const SUBMIT_CHANGE_PASSWORD_MUTATION = /* GraphQL */ ` + mutation ChangePassword($input: ResetPasswordInput!) { + customer { + resetPassword(input: $input) { + __typename + errors { + __typename + ... on Error { + message + } + } + } + } + } +`; + +export const submitChangePassword = async ({ + newPassword, + token, + customerEntityId, +}: SubmitChangePassword) => { + const mutation = graphql(SUBMIT_CHANGE_PASSWORD_MUTATION); + + const variables = { + input: { + token, + customerEntityId, + newPassword, + }, + }; + + const response = await client.fetch({ + document: mutation, + variables, + }); + + return response.data; +}; diff --git a/apps/core/client/mutations/submit-reset-password.ts b/apps/core/client/mutations/submit-reset-password.ts new file mode 100644 index 000000000..6106468eb --- /dev/null +++ b/apps/core/client/mutations/submit-reset-password.ts @@ -0,0 +1,46 @@ +import { z } from 'zod'; + +import { client } from '..'; +import { graphql } from '../generated'; + +export const ResetPasswordSchema = z.object({ + email: z.string().email(), +}); + +type SubmitResetPassword = z.infer & { + reCaptchaToken?: string; +}; + +const SUBMIT_RESET_PASSWORD_MUTATION = /* GraphQL */ ` + mutation ResetPassword($input: RequestResetPasswordInput!, $reCaptcha: ReCaptchaV2Input) { + customer { + requestResetPassword(input: $input, reCaptchaV2: $reCaptcha) { + __typename + errors { + __typename + ... on Error { + message + } + } + } + } + } +`; + +export const submitResetPassword = async ({ email, reCaptchaToken }: SubmitResetPassword) => { + const mutation = graphql(SUBMIT_RESET_PASSWORD_MUTATION); + + const variables = { + input: { + email, + }, + ...(reCaptchaToken && { reCaptchaV2: { token: reCaptchaToken } }), + }; + + const response = await client.fetch({ + document: mutation, + variables, + }); + + return response.data; +};