Skip to content

Commit

Permalink
fix(clerk-react): Use derived state over local state in useAuth (#4715)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacekradko authored Dec 10, 2024
1 parent 4ed3754 commit 0266f6a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-cats-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-react': patch
---

`useAuth` now uses derived auth state instead of locally stored state
24 changes: 24 additions & 0 deletions packages/react/src/hooks/__tests__/useAuth.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { render } from '@testing-library/react';
import React from 'react';

import { useAuth } from '../useAuth';

const TestComponent = () => {
const { isLoaded, isSignedIn } = useAuth();
return (
<div>
{isLoaded}
{isSignedIn}
</div>
);
};

describe('useAuth', () => {
test('throws an error if not wrapped in <ClerkProvider>', () => {
expect(() => {
render(<TestComponent />);
}).toThrow(
'@clerk/clerk-react: useAuth can only be used within the <ClerkProvider /> component. Learn more: https://clerk.com/docs/components/clerk-provider',
);
});
});
24 changes: 7 additions & 17 deletions packages/react/src/hooks/useAuth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createCheckAuthorization } from '@clerk/shared/authorization';
import type { CheckAuthorizationWithCustomPermissions, GetToken, SignOut, UseAuthReturn } from '@clerk/types';
import { useCallback, useEffect, useState } from 'react';
import { useCallback } from 'react';

import { useAuthContext } from '../contexts/AuthContext';
import { useIsomorphicClerkContext } from '../contexts/IsomorphicClerkContext';
Expand Down Expand Up @@ -50,24 +50,14 @@ type UseAuth = (initialAuthState?: any) => UseAuthReturn;
export const useAuth: UseAuth = (initialAuthState = {}) => {
useAssertWrappedByClerkProvider('useAuth');

const authContext = useAuthContext();
const authContextFromHook = useAuthContext();
let authContext = authContextFromHook;

const [authState, setAuthState] = useState(() => {
// This indicates the authContext is not available, and so we fallback to the provided initialState
if (authContext.sessionId === undefined && authContext.userId === undefined) {
return initialAuthState ?? {};
}
return authContext;
});

useEffect(() => {
if (authContext.sessionId === undefined && authContext.userId === undefined) {
return;
}
setAuthState(authContext);
}, [authContext]);
if (authContext.sessionId === undefined && authContext.userId === undefined) {
authContext = initialAuthState != null ? initialAuthState : {};
}

const { sessionId, userId, actor, orgId, orgRole, orgSlug, orgPermissions, factorVerificationAge } = authState;
const { sessionId, userId, actor, orgId, orgRole, orgSlug, orgPermissions, factorVerificationAge } = authContext;
const isomorphicClerk = useIsomorphicClerkContext();

const getToken: GetToken = useCallback(createGetToken(isomorphicClerk), [isomorphicClerk]);
Expand Down

0 comments on commit 0266f6a

Please sign in to comment.