Skip to content

Conversation

@panteliselef
Copy link
Member

@panteliselef panteliselef commented Feb 11, 2025

Description

Seems like our broadcasting logic inside setActive really depended on having the client being deleted and so that updateClient would never be called to update Clerk.user and Clerk.session.

Since Long lived clients, we are no longer deleting the client, which means updateClient would run before setActive causing the broadcasting logic to always be skipped.

The solution the PR suggests is to explicitly broadcast when signOut() is called. As in improvement, we should broadcast the sign out event when user.delete() gets called.

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

@changeset-bot
Copy link

changeset-bot bot commented Feb 11, 2025

🦋 Changeset detected

Latest commit: 3cd011e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@clerk/clerk-js Patch
@clerk/chrome-extension Patch
@clerk/clerk-expo Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Feb 11, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
clerk-js-sandbox ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 18, 2025 9:34am

Comment on lines -881 to -918
// If this.session exists, then signOut was triggered by the current tab
// and should emit. Other tabs should not emit the same event again
const shouldSignOutSession = this.session && newSession === null;
if (shouldSignOutSession) {
this.#broadcastSignOutEvent();
eventBus.dispatch(events.TokenUpdate, { token: null });
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Since we are no longer delete the client on sign out, this.session would always be null, causing broadcasting to never occur.

Also removing eventBus.dispatch(events.TokenUpdate, { token: null }) does not cause issues, because the code a few lines below will handle it appropriately.

@panteliselef panteliselef self-assigned this Feb 13, 2025
});

await mainTab.po.expect.toBeSignedOut();
await mainTab.po.expect.toBeSignedOut({ timeOut: 2 * 1_000 });
Copy link
Member Author

Choose a reason for hiding this comment

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

Previously the test was passing after 40-50 seconds because either /touch or /tokens would force update the state.

Comment on lines 377 to 379

// Notify other tabs that user is signing out.
this.__internal_broadcastSignOutEvent();
Copy link
Member Author

Choose a reason for hiding this comment

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

Explicitly broadcast inside signOut() instead of attempting to detect if setActive({ session: null}) should notify

});
};

// TODO: Deprecate this one, and mark it as internal. Is there actual benefit for external developers to use this ? Should they ever reach for it ?
Copy link
Member Author

Choose a reason for hiding this comment

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

What do you think about this ? Would we ever ask people to use this in a custom flow ?

Copy link
Member

Choose a reason for hiding this comment

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

Most likely not, it should really be something that's handled internally.

};

#broadcastSignOutEvent = () => {
public __internal_broadcastSignOutEvent = () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Alternatively we could use the event bus

Copy link
Member

Choose a reason for hiding this comment

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

👍 let's use the event bus, that way we can avoid a new Clerk method.

@panteliselef panteliselef changed the title fix(clerk-js): Avoid updating client when removing all sessions fix(clerk-js): Broadcast sign out to all tabs on long lived clients Feb 17, 2025
@panteliselef panteliselef marked this pull request as ready for review February 17, 2025 20:44
};

#broadcastSignOutEvent = () => {
public __internal_broadcastSignOutEvent = () => {
Copy link
Member

Choose a reason for hiding this comment

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

👍 let's use the event bus, that way we can avoid a new Clerk method.

});
};

// TODO: Deprecate this one, and mark it as internal. Is there actual benefit for external developers to use this ? Should they ever reach for it ?
Copy link
Member

Choose a reason for hiding this comment

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

Most likely not, it should really be something that's handled internally.

path: this.path() + '/sessions',
}) as unknown as Promise<ClientResource>;
}).then(e => {
SessionTokenCache.clear();
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is the right place for this call. What's the desired behavior? SessionTokenCache is cleared on sign out?

Copy link
Member Author

Choose a reason for hiding this comment

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

Correct, clearing the cache on sign out.

We're calling SessionTokenCache.clear() on Client.destroy(), on Session.end(), and on Session.remove(). Giving the fact that we clear the cache on an individual session removal, i think we should do the same when removing all of them.

Comment on lines +378 to +379
// Notify other tabs that user is signing out.
eventBus.dispatch(events.UserSignOut, null);
Copy link
Member

Choose a reason for hiding this comment

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

❓ should we be doing this later in the sign out flow?

Copy link
Member Author

Choose a reason for hiding this comment

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

Previously inside setActive it was one of the first things that got called. And since by that point we have already cleared cookies seems appropriate.


export const events = {
TokenUpdate: 'token:update',
UserSignOut: 'user:signOut',
Copy link
Member

Choose a reason for hiding this comment

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

💭 (optional) what do you think about just calling this even signOut? I'm not sure we need the user: scope.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking the same, I added the scope to respect the pattern. Since it does not do any harm, I think I'll leave it as is.

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough, I wouldn't say 1 event indicates a pattern though 😉

@panteliselef panteliselef requested a review from brkalow February 18, 2025 21:13
@panteliselef panteliselef merged commit 14c1bfa into main Feb 19, 2025
29 checks passed
@panteliselef panteliselef deleted the elef/fix-remove-sessions-client-update branch February 19, 2025 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants