Skip to content

Commit

Permalink
feat(core): add change password mutation for logged in customer
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-alexsaiannyi committed Mar 22, 2024
1 parent 30f6515 commit d0894e5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-rocks-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": patch
---

add change password mutation for logged in customer
63 changes: 63 additions & 0 deletions apps/core/client/mutations/submit-customer-change-password.ts
Original file line number Diff line number Diff line change
@@ -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<typeof Input>;

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;
};

0 comments on commit d0894e5

Please sign in to comment.