-
Notifications
You must be signed in to change notification settings - Fork 219
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 mutations for reset password (#628)
- Loading branch information
1 parent
ed23006
commit e35d947
Showing
3 changed files
with
105 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 mutations for reset password |
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,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; | ||
}; |
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,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; | ||
}; |