Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading