javascript icon indicating copy to clipboard operation
javascript copied to clipboard

Should rootAuthLoader support the loadUser option?

Open tmcw opened this issue 2 years ago • 13 comments

I recently integrated Clerk into my application, which also uses a relational database, so it needed to use Clerk's externalId setting to cross-reference. So, I then used rootAuthLoader with Remix, and the loadUser option to get the user object, to get the externalId.

This worked in development but is a hazard for production: Clerk's rate limit is 5 reqs/second, so once any website goes above that, you start randomly showing users logged-out states and throwing errors.

The eventual solution - helpfully shown by Clerk's support, which has been great - is to store the necessary data in the user session to limit database access. However, I think it's probably a good idea to guard against other users falling into this trap: 5 reqs / second is a very surprisingly low rate limit, and it very much makes it so that you shouldn't ever require a request to clerk for each pageload.

  • [x] Review the documentation: https://clerk.com/docs
  • [x] Go through package changelog files.

tmcw avatar Apr 06 '23 16:04 tmcw

Hello @tmcw, thank you for making this comment. We have an internal ticket to tackle some issues with the options passed in our middlewares/ loaders / ... across all of our supported SDKs. It seems that there are some inconsistencies based on framework limitations and other reasons. I leave this issue open and reply when we have finalized our decisions and our API designs to provide a better answer to this.

dimkl avatar Apr 10 '23 15:04 dimkl

Thanks. Trying to dodge the rate limit is really our main struggle with Clerk right now. I totally understand why it'd be a good idea to rate-limit login attempts, but for a method like getting a user object or a jwt token, such a brutal rate limit means that we're needing to add caches and workarounds that we didn't anticipate having to build.

tmcw avatar Apr 10 '23 16:04 tmcw

Spoke to tmcw regarding the rate limiting so we should be good on that front.

perkinsjr avatar Apr 10 '23 23:04 perkinsjr

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 10 days.

clerk-cookie avatar Jun 14 '23 12:06 clerk-cookie

Unless I'm missing something, I still think this issue is valid: if you use rootAuthLoader with the loadUser option, your site will break quickly because of the rate limit. We switched to not using loadUser and trying to rely on the session object, but if loadUser is a footgun, it should probably be documented as such or removed.

tmcw avatar Jun 14 '23 15:06 tmcw

It's still missing. I have marked it as not stale.

dimkl avatar Jun 14 '23 15:06 dimkl

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 10 days.

clerk-cookie avatar Jul 14 '23 18:07 clerk-cookie

Keeping this open for the same reasons as above.

tmcw avatar Jul 14 '23 18:07 tmcw

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 10 days.

clerk-cookie avatar Aug 14 '23 06:08 clerk-cookie

I… wish that this bot would be turned off or this issue would be solved, it's annoying to have to comment to keep this very clear issue open, and have to advocate for a basic feature/doc improvement on a paid product.

tmcw avatar Aug 14 '23 13:08 tmcw

I'm so sorry @tmcw - we're going to look into getting this one closed out soon!

jescalan avatar Aug 25 '23 16:08 jescalan

Is this still prioritized?

tmcw avatar Jan 05 '24 18:01 tmcw

Hey @tmcw! It is, but it got lost in the backlog for ~2.5 months unfortunately. It was recovered about 2 weeks ago and is marked as a documentation improvement at the moment along with a couple deprecation warnings.

jescalan avatar Jan 05 '24 20:01 jescalan

Thanks @tmcw for mentioning this issue! We have added a deprecation warning on these properties and will be removed in the next major version of @clerk/remix.

The correct solution is the one that is already proposed in the original post: Add the external id info to the session claims and not use any of the loadX functions.

How to setup session claims: https://clerk.com/docs/backend-requests/making/custom-session-token

Example: Screenshot 2024-05-24 at 19 09 34

Then use it in the rootAuthLoader like this:

export const loader = (args: LoaderFunctionArgs) => rootAuthLoader(args, ({ request }) => {
  const claims = request.auth.sessionClaims;
  if (claims?.extid === null) {
    // do something
  }
  return {
    // data
  };
});

anagstef avatar May 27 '24 15:05 anagstef

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

clerk-cookie avatar May 28 '25 00:05 clerk-cookie