-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add change password mutation for logged in customer (#683)
- Loading branch information
1 parent
29afe35
commit cfab55b
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
63
apps/core/client/mutations/submit-customer-change-password.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |