Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fresh-signins-reset.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Fix `useSignIn()` returning a stale resource after resetting sign-in and starting a new SSO flow.
2 changes: 1 addition & 1 deletion packages/clerk-js/src/core/resources/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class Client extends BaseResource implements ClientResource {
this.signUp = new SignUp(data.sign_up);
}

if (data.sign_in && this.signIn instanceof SignIn && this.signIn.id === data.sign_in.id) {
if (data.sign_in && this.signIn instanceof SignIn && (this.signIn.id === data.sign_in.id || !this.signIn.id)) {
this.signIn.__internal_updateFromJSON(data.sign_in);
} else {
this.signIn = new SignIn(data.sign_in);
Expand Down
38 changes: 38 additions & 0 deletions packages/clerk-js/src/core/resources/__tests__/Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,44 @@ describe('Client Singleton', () => {
expect(client.signIn.id).toBe('test_sign_in_id_v2');
});

it('preserves sign in identity after resetSignIn when fromJSON receives a new attempt', () => {
const user = createUser({ first_name: 'John', last_name: 'Doe', id: 'user_1' });
const initialClientJSON: ClientJSON = {
object: 'client',
id: 'test_id',
status: 'active',
last_active_session_id: null,
sign_in: createSignIn({ id: 'test_sign_in_id', status: 'needs_first_factor' }, user),
sign_up: null,
sessions: [],
created_at: Date.now() - 1000,
updated_at: Date.now(),
} as any;

// @ts-expect-error We cannot mess with the singleton when tests are running in parallel
const client = new Client(initialClientJSON);
client.resetSignIn();
const resetSignIn = client.signIn;

client.fromJSON({
...initialClientJSON,
sign_in: createSignIn(
{
id: 'test_sign_in_id_v2',
status: 'needs_second_factor',
identifier: 'updated@example.com',
},
user,
),
updated_at: Date.now() + 1000,
});

expect(client.signIn).toBe(resetSignIn);
expect(client.signIn.id).toBe('test_sign_in_id_v2');
expect(client.signIn.identifier).toBe('updated@example.com');
expect(client.signIn.status).toBe('needs_second_factor');
});

it('has the same initial properties', () => {
const clientJSON = {
object: 'client',
Expand Down