Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add change password mutation for logged in customer #683

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we should match the mutation name. 🍹

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 ({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, I feel we should match the mutation name.

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