Skip to content

Commit f8b38b7

Browse files
authored
feat(clerk-js): Signal reset password flow (#6520)
1 parent 05509a7 commit f8b38b7

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

.changeset/wise-hornets-sneeze.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@clerk/clerk-js': minor
3+
'@clerk/types': minor
4+
---
5+
6+
[Experimental] Signals reset password flow

packages/clerk-js/bundlewatch.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"files": [
33
{ "path": "./dist/clerk.js", "maxSize": "622KB" },
4-
{ "path": "./dist/clerk.browser.js", "maxSize": "75KB" },
4+
{ "path": "./dist/clerk.browser.js", "maxSize": "76KB" },
55
{ "path": "./dist/clerk.legacy.browser.js", "maxSize": "117KB" },
66
{ "path": "./dist/clerk.headless*.js", "maxSize": "58KB" },
77
{ "path": "./dist/ui-common*.js", "maxSize": "113KB" },

packages/clerk-js/src/core/resources/SignIn.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,12 +497,82 @@ class SignInFuture implements SignInFutureResource {
497497
verifyCode: this.verifyEmailCode.bind(this),
498498
};
499499

500+
resetPasswordEmailCode = {
501+
sendCode: this.sendResetPasswordEmailCode.bind(this),
502+
verifyCode: this.verifyResetPasswordEmailCode.bind(this),
503+
submitPassword: this.submitResetPassword.bind(this),
504+
};
505+
500506
constructor(readonly resource: SignIn) {}
501507

502508
get status() {
503509
return this.resource.status;
504510
}
505511

512+
async sendResetPasswordEmailCode(): Promise<{ error: unknown }> {
513+
eventBus.emit('resource:error', { resource: this.resource, error: null });
514+
try {
515+
if (!this.resource.id) {
516+
throw new Error('Cannot reset password without a sign in.');
517+
}
518+
519+
const resetPasswordEmailCodeFactor = this.resource.supportedFirstFactors?.find(
520+
f => f.strategy === 'reset_password_email_code',
521+
);
522+
523+
if (!resetPasswordEmailCodeFactor) {
524+
throw new Error('Reset password email code factor not found');
525+
}
526+
527+
const { emailAddressId } = resetPasswordEmailCodeFactor;
528+
await this.resource.__internal_basePost({
529+
body: { emailAddressId, strategy: 'reset_password_email_code' },
530+
action: 'prepare_first_factor',
531+
});
532+
} catch (err: unknown) {
533+
eventBus.emit('resource:error', { resource: this.resource, error: err });
534+
return { error: err };
535+
}
536+
537+
return { error: null };
538+
}
539+
540+
async verifyResetPasswordEmailCode({ code }: { code: string }): Promise<{ error: unknown }> {
541+
eventBus.emit('resource:error', { resource: this.resource, error: null });
542+
try {
543+
await this.resource.__internal_basePost({
544+
body: { code, strategy: 'reset_password_email_code' },
545+
action: 'attempt_first_factor',
546+
});
547+
} catch (err: unknown) {
548+
eventBus.emit('resource:error', { resource: this.resource, error: err });
549+
return { error: err };
550+
}
551+
552+
return { error: null };
553+
}
554+
555+
async submitResetPassword({
556+
password,
557+
signOutOfOtherSessions = true,
558+
}: {
559+
password: string;
560+
signOutOfOtherSessions?: boolean;
561+
}): Promise<{ error: unknown }> {
562+
eventBus.emit('resource:error', { resource: this.resource, error: null });
563+
try {
564+
await this.resource.__internal_basePost({
565+
body: { password, signOutOfOtherSessions },
566+
action: 'reset_password',
567+
});
568+
} catch (err: unknown) {
569+
eventBus.emit('resource:error', { resource: this.resource, error: err });
570+
return { error: err };
571+
}
572+
573+
return { error: null };
574+
}
575+
506576
async create(params: {
507577
identifier?: string;
508578
strategy?: OAuthStrategy | 'saml' | 'enterprise_sso';

packages/types/src/signIn.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ export interface SignInFutureResource {
133133
sendCode: (params: { email: string }) => Promise<{ error: unknown }>;
134134
verifyCode: (params: { code: string }) => Promise<{ error: unknown }>;
135135
};
136+
resetPasswordEmailCode: {
137+
sendCode: () => Promise<{ error: unknown }>;
138+
verifyCode: (params: { code: string }) => Promise<{ error: unknown }>;
139+
submitPassword: (params: { password: string; signOutOfOtherSessions?: boolean }) => Promise<{ error: unknown }>;
140+
};
136141
sso: (params: {
137142
flow?: 'auto' | 'modal';
138143
strategy: OAuthStrategy | 'saml' | 'enterprise_sso';

0 commit comments

Comments
 (0)