;
const initialAuthState = rest as any;
const authContextFromHook = useAuthContext();
+ const isomorphicClerk = useIsomorphicClerkContext();
let authContext = authContextFromHook;
- if (authContext.sessionId === undefined && authContext.userId === undefined) {
+ if (suspense) {
+ if (!isomorphicClerk.loaded) {
+ // eslint-disable-next-line @typescript-eslint/only-throw-error -- React Suspense requires throwing a promise
+ throw createClerkLoadedSuspensePromise(isomorphicClerk);
+ }
+
+ if (authContext.sessionId === undefined && authContext.userId === undefined) {
+ // eslint-disable-next-line @typescript-eslint/only-throw-error -- React Suspense requires throwing a promise
+ throw createTransitiveStateSuspensePromise(isomorphicClerk, authContext);
+ }
+ }
+
+ if (!isomorphicClerk.loaded && authContext.sessionId === undefined && authContext.userId === undefined) {
authContext = initialAuthState != null ? initialAuthState : {};
}
- const isomorphicClerk = useIsomorphicClerkContext();
- const getToken: GetToken = useCallback(createGetToken(isomorphicClerk), [isomorphicClerk]);
- const signOut: SignOut = useCallback(createSignOut(isomorphicClerk), [isomorphicClerk]);
+ const getToken: GetToken = useCallback(opts => createGetToken(isomorphicClerk)(opts), [isomorphicClerk]);
+ const signOut: SignOut = useCallback(opts => createSignOut(isomorphicClerk)(opts), [isomorphicClerk]);
- isomorphicClerk.telemetry?.record(eventMethodCalled('useAuth', { treatPendingAsSignedOut }));
+ isomorphicClerk.telemetry?.record(eventMethodCalled('useAuth', { suspense, treatPendingAsSignedOut }));
return useDerivedAuth(
{
diff --git a/packages/shared/src/types/session.ts b/packages/shared/src/types/session.ts
index 11629a838d4..7c61b40f016 100644
--- a/packages/shared/src/types/session.ts
+++ b/packages/shared/src/types/session.ts
@@ -38,6 +38,12 @@ export type PendingSessionOptions = {
* @default true
*/
treatPendingAsSignedOut?: boolean;
+ /**
+ * When true, the hook will suspend while Clerk is loading instead of returning `isLoaded: false`.
+ * Requires a React Suspense boundary to be present in the component tree.
+ * @default false
+ */
+ suspense?: boolean;
};
type DisallowSystemPermissions = P extends `${OrganizationSystemPermissionPrefix}${string}`