Skip to content

Commit

Permalink
isLoading -> !isReady
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjmcgrath committed May 28, 2020
1 parent 1a443f2 commit a5c9ef6
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 25 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ReactDOM.render(
);
```

Use the `useAuth0` hook in your components to access authentication state (`isLoading`, `isAuthenticated` and `user`) and authentication methods (`loginWithRedirect` and `logout`):
Use the `useAuth0` hook in your components to access authentication state (`isReady`, `isAuthenticated` and `user`) and authentication methods (`loginWithRedirect` and `logout`):

```jsx
// src/App.js
Expand All @@ -68,15 +68,15 @@ import { useAuth0 } from '@auth0/auth0-react';

function App() {
const {
isLoading,
isReady,
isAuthenticated,
error,
user,
loginWithRedirect,
logout,
} = useAuth0();

if (isLoading) {
if (!isReady) {
return <div>Loading...</div>;
}
if (error) {
Expand Down
12 changes: 6 additions & 6 deletions __tests__/auth-provider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ describe('Auth0Provider', () => {
() => useContext(Auth0Context),
{ wrapper }
);
expect(result.current.isLoading).toBe(true);
expect(result.current.isReady).toBe(false);
await waitForNextUpdate();
expect(result.current.isLoading).toBe(false);
expect(result.current.isReady).toBe(true);
expect(clientMock.getTokenSilently).toHaveBeenCalled();
expect(result.current.isAuthenticated).toBe(false);
});
Expand Down Expand Up @@ -158,9 +158,9 @@ describe('Auth0Provider', () => {
act(() => {
result.current.loginWithPopup();
});
expect(result.current.isLoading).toBe(true);
expect(result.current.isReady).toBe(false);
await waitForNextUpdate();
expect(result.current.isLoading).toBe(false);
expect(result.current.isReady).toBe(true);
expect(clientMock.loginWithPopup).toHaveBeenCalled();
expect(result.current.isAuthenticated).toBe(true);
});
Expand All @@ -179,9 +179,9 @@ describe('Auth0Provider', () => {
act(() => {
result.current.loginWithPopup();
});
expect(result.current.isLoading).toBe(true);
expect(result.current.isReady).toBe(false);
await waitForNextUpdate();
expect(result.current.isLoading).toBe(false);
expect(result.current.isReady).toBe(true);
expect(clientMock.loginWithPopup).toHaveBeenCalled();
expect(result.current.isAuthenticated).toBe(false);
expect(() => {
Expand Down
6 changes: 3 additions & 3 deletions __tests__/auth-reducer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('reducer', () => {
reducer(initialAuthState, { type: 'INITIALISED', ...payload })
).toEqual({
...initialAuthState,
isLoading: false,
isReady: true,
...payload,
});
});
Expand All @@ -24,7 +24,7 @@ describe('reducer', () => {
reducer(initialAuthState, { type: 'INITIALISED', ...payload })
).toEqual({
...initialAuthState,
isLoading: false,
isReady: true,
...payload,
});
});
Expand All @@ -35,7 +35,7 @@ describe('reducer', () => {
};
expect(reducer(initialAuthState, { type: 'ERROR', ...payload })).toEqual({
...initialAuthState,
isLoading: false,
isReady: true,
...payload,
});
});
Expand Down
4 changes: 2 additions & 2 deletions __tests__/ssr.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ describe('In a Node SSR environment', () => {
const wrapper = createWrapper();
const {
result: {
current: { isLoading, isAuthenticated, user, loginWithRedirect },
current: { isReady, isAuthenticated, user, loginWithRedirect },
},
} = renderHook(useAuth0, { wrapper });
expect(isLoading).toBeFalsy();
expect(isReady).toBeTruthy();
expect(isAuthenticated).toBeFalsy();
expect(user).toBeUndefined();
await expect(loginWithRedirect).rejects.toThrowError(
Expand Down
6 changes: 3 additions & 3 deletions src/auth-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ export type User = any; // eslint-disable-line @typescript-eslint/no-explicit-an
export interface AuthState {
error?: Error;
isAuthenticated: boolean;
isLoading: boolean;
isReady: boolean;
user?: User;
}

export const initialAuthState: AuthState = {
isAuthenticated: false,
// In SSR mode the library will never check the session, so loading should be initialised as false
isLoading: typeof window !== 'undefined',
// In SSR mode the library will never check the session, so it should be initialised as ready
isReady: typeof window === 'undefined',
};
6 changes: 3 additions & 3 deletions src/reducer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ export const reducer = (state: AuthState, action: Action): AuthState => {
case 'LOGIN_POPUP_STARTED':
return {
...state,
isLoading: true,
isReady: false,
};
case 'LOGIN_POPUP_COMPLETE':
case 'INITIALISED':
return {
...state,
isAuthenticated: action.isAuthenticated,
user: action.user,
isLoading: false,
isReady: true,
};
case 'ERROR':
return {
...state,
isLoading: false,
isReady: true,
error: action.error,
};
}
Expand Down
6 changes: 3 additions & 3 deletions src/with-login-required.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ const withAuthenticationRequired = <P extends object>(
Component: ComponentType<P>,
onRedirecting: () => JSX.Element = defaultOnRedirecting
): FC<P> => (props: P): JSX.Element => {
const { isAuthenticated, isLoading, loginWithRedirect } = useAuth0();
const { isAuthenticated, isReady, loginWithRedirect } = useAuth0();

useEffect(() => {
if (isLoading || isAuthenticated) {
if (!isReady || isAuthenticated) {
return;
}
(async (): Promise<void> => {
await loginWithRedirect({
appState: { returnTo: window.location.pathname },
});
})();
}, [isLoading, isAuthenticated, loginWithRedirect]);
}, [isReady, isAuthenticated, loginWithRedirect]);

return isAuthenticated ? <Component {...props} /> : onRedirecting();
};
Expand Down
4 changes: 2 additions & 2 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
const App = () => {
const {
isAuthenticated,
isLoading,
isReady,
getIdTokenClaims,
getAccessTokenSilently,
getAccessTokenWithPopup,
Expand All @@ -40,7 +40,7 @@
const [token, setToken] = useState(null);
const [claims, setClaims] = useState(null);

if (isLoading) {
if (!isReady) {
return <div>loading...</div>;
}

Expand Down

0 comments on commit a5c9ef6

Please sign in to comment.