Skip to content

Commit

Permalink
Check if client has loaded before saying that loading is finished (#7608
Browse files Browse the repository at this point in the history
)

* Check if client has loaded before saying that loading is finished

* Get rid of ClerkLoaded
  • Loading branch information
Tobbe authored and jtoar committed Feb 13, 2023
1 parent 284b429 commit 279a314
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect } from 'react'

import { ClerkLoaded, ClerkProvider, useUser } from '@clerk/clerk-react'
import { ClerkProvider, useUser } from '@clerk/clerk-react'

import { createAuth } from '@redwoodjs/auth-clerk-web'
import { navigate } from '@redwoodjs/router'
Expand Down Expand Up @@ -40,7 +40,7 @@ export const AuthProvider = ({ children }: Props) => {
return (
<ClerkProvider {...clerkOptions} navigate={(to) => navigate(to)}>
<ClerkRwAuthProvider>
<ClerkLoaded>{children}</ClerkLoaded>
{children}
<ClerkStatusUpdater />
</ClerkRwAuthProvider>
</ClerkProvider>
Expand Down
3 changes: 3 additions & 0 deletions packages/auth-providers/clerk/web/src/clerk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,8 @@ function createAuthImplementation() {
const clerk = (window as any).Clerk as Clerk
return clerk?.user
},
clientHasLoaded: () => {
return (window as any).Clerk !== undefined
},
}
}
1 change: 1 addition & 0 deletions packages/auth/src/AuthImplementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface AuthImplementation<
forgotPassword?(username: string): Promise<TForgotPassword>
resetPassword?(options?: unknown): Promise<TResetPassword>
validateResetToken?(token: string | null): Promise<TValidateResetToken>
clientHasLoaded?(): boolean

/**
* The user's data from the AuthProvider
Expand Down
7 changes: 7 additions & 0 deletions packages/auth/src/AuthProvider/useReauthenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,15 @@ export const useReauthenticate = <TUser>(
const userMetadata = await authImplementation.getUserMetadata()

if (!userMetadata) {
let loading = false

if (authImplementation.clientHasLoaded) {
loading = !authImplementation.clientHasLoaded()
}

setAuthProviderState({
...notAuthenticatedState,
loading,
client: authImplementation.client,
})
} else {
Expand Down

0 comments on commit 279a314

Please sign in to comment.