Skip to content
6 changes: 6 additions & 0 deletions .changeset/twelve-crabs-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@clerk/clerk-js': minor
'@clerk/types': minor
---

[Experimental] Signal `fetchStatus` support.
3 changes: 3 additions & 0 deletions packages/clerk-js/src/core/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ export const events = {
SessionTokenResolved: 'session:tokenResolved',
ResourceUpdate: 'resource:update',
ResourceError: 'resource:error',
ResourceFetch: 'resource:fetch',
} as const;

type TokenUpdatePayload = { token: TokenResource | null };
export type ResourceUpdatePayload = { resource: BaseResource };
export type ResourceErrorPayload = { resource: BaseResource; error: unknown };
export type ResourceFetchPayload = { resource: BaseResource; status: 'idle' | 'fetching' };

type InternalEvents = {
[events.TokenUpdate]: TokenUpdatePayload;
Expand All @@ -23,6 +25,7 @@ type InternalEvents = {
[events.SessionTokenResolved]: null;
[events.ResourceUpdate]: ResourceUpdatePayload;
[events.ResourceError]: ResourceErrorPayload;
[events.ResourceFetch]: ResourceFetchPayload;
};

export const eventBus = createEventBus<InternalEvents>();
100 changes: 23 additions & 77 deletions packages/clerk-js/src/core/resources/SignIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
ResetPasswordParams,
ResetPasswordPhoneCodeFactorConfig,
SamlConfig,
SetActiveNavigate,
SignInCreateParams,
SignInFirstFactor,
SignInFutureResource,
Expand Down Expand Up @@ -58,6 +59,7 @@ import {
webAuthnGetCredential as webAuthnGetCredentialOnWindow,
} from '../../utils/passkeys';
import { createValidatePassword } from '../../utils/passwords/password';
import { runAsyncResourceTask } from '../../utils/runAsyncResourceTask';
import {
clerkInvalidFAPIResponse,
clerkInvalidStrategy,
Expand Down Expand Up @@ -493,8 +495,6 @@ class SignInFuture implements SignInFutureResource {
submitPassword: this.submitResetPassword.bind(this),
};

fetchStatus: 'idle' | 'fetching' = 'idle';

constructor(readonly resource: SignIn) {}

get status() {
Expand All @@ -506,8 +506,7 @@ class SignInFuture implements SignInFutureResource {
}

async sendResetPasswordEmailCode(): Promise<{ error: unknown }> {
eventBus.emit('resource:error', { resource: this.resource, error: null });
try {
return runAsyncResourceTask(this.resource, async () => {
if (!this.resource.id) {
throw new Error('Cannot reset password without a sign in.');
}
Expand All @@ -525,27 +524,16 @@ class SignInFuture implements SignInFutureResource {
body: { emailAddressId, strategy: 'reset_password_email_code' },
action: 'prepare_first_factor',
});
} catch (err: unknown) {
eventBus.emit('resource:error', { resource: this.resource, error: err });
return { error: err };
}

return { error: null };
});
}

async verifyResetPasswordEmailCode({ code }: { code: string }): Promise<{ error: unknown }> {
eventBus.emit('resource:error', { resource: this.resource, error: null });
try {
return runAsyncResourceTask(this.resource, async () => {
await this.resource.__internal_basePost({
body: { code, strategy: 'reset_password_email_code' },
action: 'attempt_first_factor',
});
} catch (err: unknown) {
eventBus.emit('resource:error', { resource: this.resource, error: err });
return { error: err };
}

return { error: null };
});
}

async submitResetPassword({
Expand All @@ -555,18 +543,12 @@ class SignInFuture implements SignInFutureResource {
password: string;
signOutOfOtherSessions?: boolean;
}): Promise<{ error: unknown }> {
eventBus.emit('resource:error', { resource: this.resource, error: null });
try {
return runAsyncResourceTask(this.resource, async () => {
await this.resource.__internal_basePost({
body: { password, signOutOfOtherSessions },
action: 'reset_password',
});
} catch (err: unknown) {
eventBus.emit('resource:error', { resource: this.resource, error: err });
return { error: err };
}

return { error: null };
});
}

async create(params: {
Expand All @@ -575,39 +557,26 @@ class SignInFuture implements SignInFutureResource {
redirectUrl?: string;
actionCompleteRedirectUrl?: string;
}): Promise<{ error: unknown }> {
eventBus.emit('resource:error', { resource: this.resource, error: null });
try {
return runAsyncResourceTask(this.resource, async () => {
await this.resource.__internal_basePost({
path: this.resource.pathRoot,
body: params,
});

return { error: null };
} catch (err: unknown) {
eventBus.emit('resource:error', { resource: this.resource, error: err });
return { error: err };
}
});
}

async password({ identifier, password }: { identifier?: string; password: string }): Promise<{ error: unknown }> {
eventBus.emit('resource:error', { resource: this.resource, error: null });
const previousIdentifier = this.resource.identifier;
try {
return runAsyncResourceTask(this.resource, async () => {
const previousIdentifier = this.resource.identifier;
await this.resource.__internal_basePost({
path: this.resource.pathRoot,
body: { identifier: identifier || previousIdentifier, password },
});
} catch (err: unknown) {
eventBus.emit('resource:error', { resource: this.resource, error: err });
return { error: err };
}

return { error: null };
});
Comment on lines +570 to +575
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Avoid sending identifier: null; include the field only when defined

Using identifier || previousIdentifier can yield null, which will serialize as identifier: null. Prefer nullish coalescing and omit the key when undefined.

-      const previousIdentifier = this.resource.identifier;
-      await this.resource.__internal_basePost({
-        path: this.resource.pathRoot,
-        body: { identifier: identifier || previousIdentifier, password },
-      });
+      const previousIdentifier = this.resource.identifier;
+      const resolvedIdentifier = identifier ?? previousIdentifier ?? undefined;
+      await this.resource.__internal_basePost({
+        path: this.resource.pathRoot,
+        body: {
+          ...(resolvedIdentifier !== undefined ? { identifier: resolvedIdentifier } : {}),
+          password,
+        },
+      });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const previousIdentifier = this.resource.identifier;
await this.resource.__internal_basePost({
path: this.resource.pathRoot,
body: { identifier: identifier || previousIdentifier, password },
});
} catch (err: unknown) {
eventBus.emit('resource:error', { resource: this.resource, error: err });
return { error: err };
}
return { error: null };
});
const previousIdentifier = this.resource.identifier;
const resolvedIdentifier = identifier ?? previousIdentifier ?? undefined;
await this.resource.__internal_basePost({
path: this.resource.pathRoot,
body: {
...(resolvedIdentifier !== undefined ? { identifier: resolvedIdentifier } : {}),
password,
},
});
});
🤖 Prompt for AI Agents
In packages/clerk-js/src/core/resources/SignIn.ts around lines 569 to 574,
replace the identifier || previousIdentifier pattern (which can produce
identifier: null) with logic that uses nullish coalescing and only adds the
identifier key when defined; for example, compute const id = identifier ??
previousIdentifier and build the request body by always including password but
only adding identifier if id !== undefined (and/or !== null if you want to avoid
null explicitly), then pass that body to __internal_basePost so identifier is
omitted from the serialized payload when not defined.

}

async sendEmailCode({ email }: { email: string }): Promise<{ error: unknown }> {
eventBus.emit('resource:error', { resource: this.resource, error: null });
try {
return runAsyncResourceTask(this.resource, async () => {
if (!this.resource.id) {
await this.create({ identifier: email });
}
Expand All @@ -623,27 +592,16 @@ class SignInFuture implements SignInFutureResource {
body: { emailAddressId, strategy: 'email_code' },
action: 'prepare_first_factor',
});
} catch (err: unknown) {
eventBus.emit('resource:error', { resource: this.resource, error: err });
return { error: err };
}

return { error: null };
});
}

async verifyEmailCode({ code }: { code: string }): Promise<{ error: unknown }> {
eventBus.emit('resource:error', { resource: this.resource, error: null });
try {
return runAsyncResourceTask(this.resource, async () => {
await this.resource.__internal_basePost({
body: { code, strategy: 'email_code' },
action: 'attempt_first_factor',
});
} catch (err: unknown) {
eventBus.emit('resource:error', { resource: this.resource, error: err });
return { error: err };
}

return { error: null };
});
}

async sso({
Expand All @@ -657,8 +615,7 @@ class SignInFuture implements SignInFutureResource {
redirectUrl: string;
redirectUrlComplete: string;
}): Promise<{ error: unknown }> {
eventBus.emit('resource:error', { resource: this.resource, error: null });
try {
return runAsyncResourceTask(this.resource, async () => {
if (flow !== 'auto') {
throw new Error('modal flow is not supported yet');
}
Expand All @@ -678,27 +635,16 @@ class SignInFuture implements SignInFutureResource {
if (status === 'unverified' && externalVerificationRedirectURL) {
windowNavigate(externalVerificationRedirectURL);
}
} catch (err: unknown) {
eventBus.emit('resource:error', { resource: this.resource, error: err });
return { error: err };
}

return { error: null };
});
}

async finalize(): Promise<{ error: unknown }> {
eventBus.emit('resource:error', { resource: this.resource, error: null });
try {
async finalize({ navigate }: { navigate?: SetActiveNavigate }): Promise<{ error: unknown }> {
return runAsyncResourceTask(this.resource, async () => {
if (!this.resource.createdSessionId) {
throw new Error('Cannot finalize sign-in without a created session.');
}

await SignIn.clerk.setActive({ session: this.resource.createdSessionId });
} catch (err: unknown) {
eventBus.emit('resource:error', { resource: this.resource, error: err });
return { error: err };
}

return { error: null };
await SignIn.clerk.setActive({ session: this.resource.createdSessionId, navigate });
});
}
}
16 changes: 9 additions & 7 deletions packages/clerk-js/src/core/signals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ import { computed, signal } from 'alien-signals';

import type { SignIn } from './resources/SignIn';

export const signInSignal = signal<{ resource: SignIn | null }>({ resource: null });
export const signInResourceSignal = signal<{ resource: SignIn | null }>({ resource: null });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Rename to signInResourceSignal is clear; verify callers and mark as internal

Good separation of responsibilities by renaming to signInResourceSignal. If this is not part of the public API, add an @internal JSDoc to comply with docs guidelines and prevent accidental external usage. Also, please verify there are no lingering imports/usages of the old signInSignal.

Run this to ensure no leftover references exist:

Apply this diff to mark the export as internal:

+/** @internal */
 export const signInResourceSignal = signal<{ resource: SignIn | null }>({ resource: null });

🏁 Script executed:

#!/bin/bash
set -euo pipefail
# Find any remaining references to the old name
rg -n --heading '\bsignInSignal\b' -g '!**/node_modules/**'

Length of output: 365


Update leftover signInSignal references & mark new export as internal

We’ve got three remaining signInSignal references that need to be switched over (and the new export marked @internal):

  • packages/clerk-js/src/core/signals.ts

    +/** @internal */
     export const signInResourceSignal = signal<{ resource: SignIn | null }>({ resource: null });
  • packages/clerk-js/src/core/state.ts (line 13)

    -export const signInSignal = signInComputedSignal;
    +export const signInResourceSignal = signInComputedSignal;
  • packages/types/src/state.ts (line 32)

    -signInSignal: Signal<SignIn | null>;
    +signInResourceSignal: Signal<SignIn | null>;
  • packages/react/src/hooks/useClerkSignal.ts (lines 21 & 38)

    -clerk.__internal_state!.signInSignal();
    +clerk.__internal_state!.signInResourceSignal();
    
    -return clerk.__internal_state.signInSignal();
    +return clerk.__internal_state.signInResourceSignal();

Please make these changes to fully remove the old signInSignal and ensure the new export is marked @internal.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export const signInResourceSignal = signal<{ resource: SignIn | null }>({ resource: null });
/** @internal */
export const signInResourceSignal = signal<{ resource: SignIn | null }>({ resource: null });
🤖 Prompt for AI Agents
In packages/clerk-js/src/core/signals.ts around line 7, mark the new export
signInResourceSignal as internal by adding a JSDoc @internal comment immediately
above its declaration; then update the three remaining references of the old
signInSignal to the new name across the codebase: change usages in
packages/clerk-js/src/core/state.ts (line 13), packages/types/src/state.ts (line
32), and packages/react/src/hooks/useClerkSignal.ts (lines 21 and 38) to import
and use signInResourceSignal instead of signInSignal, ensuring any type
imports/exports are updated accordingly so imports resolve and there are no
leftover references to signInSignal.

export const signInErrorSignal = signal<{ error: unknown }>({ error: null });
export const signInFetchSignal = signal<{ status: 'idle' | 'fetching' }>({ status: 'idle' });

export const signInComputedSignal = computed(() => {
const signIn = signInSignal().resource;
const signIn = signInResourceSignal().resource;
const error = signInErrorSignal().error;
const fetchStatus = signInFetchSignal().status;

const errors = errorsToParsedErrors(error);

if (!signIn) {
return { errors, signIn: null };
}

return { errors, signIn: signIn.__internal_future };
return { errors, fetchStatus, signIn: signIn ? signIn.__internal_future : null };
});

/**
Expand All @@ -42,6 +40,10 @@ function errorsToParsedErrors(error: unknown): Errors {
global: [],
};

if (!error) {
return parsedErrors;
}

if (!isClerkAPIResponseError(error)) {
parsedErrors.raw.push(error);
parsedErrors.global.push(error);
Expand Down
12 changes: 10 additions & 2 deletions packages/clerk-js/src/core/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { computed, effect } from 'alien-signals';
import { eventBus } from './events';
import type { BaseResource } from './resources/Base';
import { SignIn } from './resources/SignIn';
import { signInComputedSignal, signInErrorSignal, signInSignal } from './signals';
import { signInComputedSignal, signInErrorSignal, signInFetchSignal, signInResourceSignal } from './signals';

export class State implements StateInterface {
signInResourceSignal = signInSignal;
signInResourceSignal = signInResourceSignal;
signInErrorSignal = signInErrorSignal;
signInFetchSignal = signInFetchSignal;
signInSignal = signInComputedSignal;

__internal_effect = effect;
Expand All @@ -17,6 +18,7 @@ export class State implements StateInterface {
constructor() {
eventBus.on('resource:update', this.onResourceUpdated);
eventBus.on('resource:error', this.onResourceError);
eventBus.on('resource:fetch', this.onResourceFetch);
}

private onResourceError = (payload: { resource: BaseResource; error: unknown }) => {
Expand All @@ -30,4 +32,10 @@ export class State implements StateInterface {
this.signInResourceSignal({ resource: payload.resource });
}
};

private onResourceFetch = (payload: { resource: BaseResource; status: 'idle' | 'fetching' }) => {
if (payload.resource instanceof SignIn) {
this.signInFetchSignal({ status: payload.status });
}
};
}
65 changes: 65 additions & 0 deletions packages/clerk-js/src/utils/__tests__/runAsyncResourceTask.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { afterEach, describe, expect, it, vi } from 'vitest';

import { eventBus } from '../../core/events';
import { runAsyncResourceTask } from '../runAsyncResourceTask';

describe('runAsyncTask', () => {
afterEach(() => {
vi.restoreAllMocks();
});

const resource = {} as any; // runAsyncTask doesn't depend on resource being a BaseResource

it('emits fetching/idle and returns result on success', async () => {
const emitSpy = vi.spyOn(eventBus, 'emit');
const task = vi.fn().mockResolvedValue('ok');

const { result, error } = await runAsyncResourceTask(resource, task);

expect(task).toHaveBeenCalledTimes(1);
expect(result).toBe('ok');
expect(error).toBeNull();

expect(emitSpy).toHaveBeenNthCalledWith(1, 'resource:error', {
resource,
error: null,
});
expect(emitSpy).toHaveBeenNthCalledWith(2, 'resource:fetch', {
resource,
status: 'fetching',
});
expect(emitSpy).toHaveBeenNthCalledWith(3, 'resource:fetch', {
resource,
status: 'idle',
});
});

it('emits error and returns error on failure', async () => {
const emitSpy = vi.spyOn(eventBus, 'emit');
const thrown = new Error('fail');
const task = vi.fn().mockRejectedValue(thrown);

const { result, error } = await runAsyncResourceTask(resource, task);

expect(task).toHaveBeenCalledTimes(1);
expect(result).toBeUndefined();
expect(error).toBe(thrown);

expect(emitSpy).toHaveBeenNthCalledWith(1, 'resource:error', {
resource,
error: null,
});
expect(emitSpy).toHaveBeenNthCalledWith(2, 'resource:fetch', {
resource,
status: 'fetching',
});
expect(emitSpy).toHaveBeenNthCalledWith(3, 'resource:error', {
resource,
error: thrown,
});
expect(emitSpy).toHaveBeenNthCalledWith(4, 'resource:fetch', {
resource,
status: 'idle',
});
});
});
Loading