fix(clerk): make useAuth
's loading
prop track the Clerk client
#7602
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I think something like this is the fix for https://community.redwoodjs.com/t/redwood-v4-0-and-clerk-dev-cannot-render-component-as-a-sessions-already-exists-redirecting-to-the-home-url/4567/11.
The gist of the problem is in the "try" part of the try-catch block in
useReauthenticate
here:redwood/packages/auth/src/AuthProvider/useReauthenticate.ts
Lines 28 to 36 in 2813199
Here's
notAuthenticatedState
for completeness:redwood/packages/auth/src/AuthProvider/useReauthenticate.ts
Lines 9 to 15 in 2813199
What this means is that if we can't get
userMetadata
, we consider the auth provider to have loaded.loading
isfalse
. But this isn't how Clerk works. Clerk isn't finished loading until the client is on window object (I think).The work-in-progress fix in this PR is simple, maybe too simple? It doesn't use React APIs. If we want to use React APIs, we should reach for Clerk's
useAuth
hook and destructure out theisLoaded
prop. But that would require changing the API ofcreateAuthImplementation
et. al a bit (I think—we could just add custom logic touseReauthenticate
since we officially support Clerk, but this would kind of go against all the work we did to decouple auth in the first place).There's a couple more changes I'd like to make in this fix:
I'm not sure that we need
ClerkLoaded
at all in the web/src/auth.tsx template; I think it may actually be bad because it blocks rendering of its children till Clerk has loaded. Ideally theAuthProvider
should handle that so the Router can display thewhileLoadingAuth
componentit sounds like Clerk's
useAuth
hook is newer and more recommended than it's useUserhook
; maybe we should go with that?See https://clerk.dev/docs/reference/clerk-react/useauth: