Skip to content

Commit

Permalink
feat(core): add mutations for reset password
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-alexsaiannyi committed Mar 12, 2024
1 parent b489521 commit 569a42a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
54 changes: 54 additions & 0 deletions apps/core/client/mutations/submit-change-password.ts
Original file line number Diff line number Diff line change
@@ -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<typeof ChangePasswordSchema>['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;
};
46 changes: 46 additions & 0 deletions apps/core/client/mutations/submit-reset-password.ts
Original file line number Diff line number Diff line change
@@ -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<typeof ResetPasswordSchema> & {
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;
};

0 comments on commit 569a42a

Please sign in to comment.