Skip to content

Commit 5b24129

Browse files
authored
fix(clerk-js,shared,types): Support calling password without identifier (#6534)
1 parent 83757e7 commit 5b24129

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

.changeset/young-cats-retire.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@clerk/clerk-js': minor
3+
'@clerk/shared': minor
4+
'@clerk/types': minor
5+
---
6+
7+
[Experimental] Signals: Add support for calling `signIn.password()` without an identifier.

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,12 +493,18 @@ class SignInFuture implements SignInFutureResource {
493493
submitPassword: this.submitResetPassword.bind(this),
494494
};
495495

496+
fetchStatus: 'idle' | 'fetching' = 'idle';
497+
496498
constructor(readonly resource: SignIn) {}
497499

498500
get status() {
499501
return this.resource.status;
500502
}
501503

504+
get availableStrategies() {
505+
return this.resource.supportedFirstFactors ?? [];
506+
}
507+
502508
async sendResetPasswordEmailCode(): Promise<{ error: unknown }> {
503509
eventBus.emit('resource:error', { resource: this.resource, error: null });
504510
try {
@@ -583,12 +589,13 @@ class SignInFuture implements SignInFutureResource {
583589
}
584590
}
585591

586-
async password({ identifier, password }: { identifier: string; password: string }): Promise<{ error: unknown }> {
592+
async password({ identifier, password }: { identifier?: string; password: string }): Promise<{ error: unknown }> {
587593
eventBus.emit('resource:error', { resource: this.resource, error: null });
594+
const previousIdentifier = this.resource.identifier;
588595
try {
589596
await this.resource.__internal_basePost({
590597
path: this.resource.pathRoot,
591-
body: { identifier, password },
598+
body: { identifier: identifier || previousIdentifier, password },
592599
});
593600
} catch (err: unknown) {
594601
eventBus.emit('resource:error', { resource: this.resource, error: err });
@@ -678,4 +685,20 @@ class SignInFuture implements SignInFutureResource {
678685

679686
return { error: null };
680687
}
688+
689+
async finalize(): Promise<{ error: unknown }> {
690+
eventBus.emit('resource:error', { resource: this.resource, error: null });
691+
try {
692+
if (!this.resource.createdSessionId) {
693+
throw new Error('Cannot finalize sign-in without a created session.');
694+
}
695+
696+
await SignIn.clerk.setActive({ session: this.resource.createdSessionId });
697+
} catch (err: unknown) {
698+
eventBus.emit('resource:error', { resource: this.resource, error: err });
699+
return { error: err };
700+
}
701+
702+
return { error: null };
703+
}
681704
}

packages/shared/src/error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export function isKnownError(error: any): error is ClerkAPIResponseError | Clerk
8282
* @internal
8383
*/
8484
export function isClerkAPIResponseError(err: any): err is ClerkAPIResponseError {
85-
return 'clerkError' in err;
85+
return err && 'clerkError' in err;
8686
}
8787

8888
/**

packages/types/src/signIn.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,16 @@ export interface SignInResource extends ClerkResource {
126126
}
127127

128128
export interface SignInFutureResource {
129+
fetchStatus: 'idle' | 'fetching';
130+
availableStrategies: SignInFirstFactor[];
129131
status: SignInStatus | null;
130-
create: (params: { identifier: string }) => Promise<{ error: unknown }>;
131-
password: (params: { identifier: string; password: string }) => Promise<{ error: unknown }>;
132+
create: (params: {
133+
identifier?: string;
134+
strategy?: OAuthStrategy | 'saml' | 'enterprise_sso';
135+
redirectUrl?: string;
136+
actionCompleteRedirectUrl?: string;
137+
}) => Promise<{ error: unknown }>;
138+
password: (params: { identifier?: string; password: string }) => Promise<{ error: unknown }>;
132139
emailCode: {
133140
sendCode: (params: { email: string }) => Promise<{ error: unknown }>;
134141
verifyCode: (params: { code: string }) => Promise<{ error: unknown }>;
@@ -144,6 +151,7 @@ export interface SignInFutureResource {
144151
redirectUrl: string;
145152
redirectUrlComplete: string;
146153
}) => Promise<{ error: unknown }>;
154+
finalize: () => Promise<{ error: unknown }>;
147155
}
148156

149157
export type SignInStatus =

0 commit comments

Comments
 (0)