-
Notifications
You must be signed in to change notification settings - Fork 80
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 for logged-in customer
- Loading branch information
1 parent
cfab55b
commit 62266a4
Showing
9 changed files
with
465 additions
and
25 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": minor | ||
--- | ||
|
||
add change password for logged-in customer |
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
57 changes: 57 additions & 0 deletions
57
apps/core/app/[locale]/(default)/login/_actions/submit-change-password-form.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,57 @@ | ||
'use server'; | ||
|
||
import { ZodError } from 'zod'; | ||
|
||
import { | ||
ChangePasswordSchema, | ||
submitChangePassword, | ||
} from '~/client/mutations/submit-change-password'; | ||
|
||
export interface State { | ||
status: 'idle' | 'error' | 'success'; | ||
message?: string; | ||
} | ||
|
||
export const submitChangePasswordForm = async (_previousState: unknown, formData: FormData) => { | ||
try { | ||
const parsedData = ChangePasswordSchema.parse({ | ||
customerId: formData.get('customer-id'), | ||
customerToken: formData.get('customer-token'), | ||
newPassword: formData.get('new-password'), | ||
confirmPassword: formData.get('confirm-password'), | ||
}); | ||
|
||
const response = await submitChangePassword({ | ||
newPassword: parsedData.newPassword, | ||
token: parsedData.customerToken, | ||
customerEntityId: Number(parsedData.customerId), | ||
}); | ||
|
||
if (response.customer.resetPassword.errors.length === 0) { | ||
return { status: 'success', message: '' }; | ||
} | ||
|
||
return { | ||
status: 'error', | ||
message: response.customer.resetPassword.errors.map((error) => error.message).join('\n'), | ||
}; | ||
} catch (error: unknown) { | ||
if (error instanceof ZodError) { | ||
return { | ||
status: 'error', | ||
message: error.issues | ||
.map(({ path, message }) => `${path.toString()}: ${message}.`) | ||
.join('\n'), | ||
}; | ||
} | ||
|
||
if (error instanceof Error) { | ||
return { | ||
status: 'error', | ||
message: error.message, | ||
}; | ||
} | ||
|
||
return { status: 'error', message: 'Unknown error' }; | ||
} | ||
}; |
58 changes: 58 additions & 0 deletions
58
apps/core/app/[locale]/(default)/login/_actions/submit-customer-change-password-form.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,58 @@ | ||
'use server'; | ||
|
||
import { ZodError } from 'zod'; | ||
|
||
import { | ||
CustomerChangePasswordSchema, | ||
submitCustomerChangePassword, | ||
} from '~/client/mutations/submit-customer-change-password'; | ||
|
||
export interface State { | ||
status: 'idle' | 'error' | 'success'; | ||
message?: string; | ||
} | ||
|
||
export const submitCustomerChangePasswordForm = async ( | ||
_previousState: unknown, | ||
formData: FormData, | ||
) => { | ||
try { | ||
const parsedData = CustomerChangePasswordSchema.parse({ | ||
newPassword: formData.get('new-password'), | ||
currentPassword: formData.get('current-password'), | ||
confirmPassword: formData.get('confirm-password'), | ||
}); | ||
|
||
const response = await submitCustomerChangePassword({ | ||
newPassword: parsedData.newPassword, | ||
currentPassword: parsedData.currentPassword, | ||
}); | ||
|
||
if (response.errors.length === 0) { | ||
return { status: 'success', message: '' }; | ||
} | ||
|
||
return { | ||
status: 'error', | ||
message: response.errors.map((error) => error.message).join('\n'), | ||
}; | ||
} catch (error: unknown) { | ||
if (error instanceof ZodError) { | ||
return { | ||
status: 'error', | ||
message: error.issues | ||
.map(({ path, message }) => `${path.toString()}: ${message}.`) | ||
.join('\n'), | ||
}; | ||
} | ||
|
||
if (error instanceof Error) { | ||
return { | ||
status: 'error', | ||
message: error.message, | ||
}; | ||
} | ||
|
||
return { status: 'error', message: 'Unknown error' }; | ||
} | ||
}; |
Oops, something went wrong.