Skip to content

Commit

Permalink
Merge pull request #5351 from WiXSL/fix-useauthstate-hook-docs
Browse files Browse the repository at this point in the history
Fix useAuthState hook docs
  • Loading branch information
djhi committed Oct 6, 2020
2 parents 2f92a4f + d74b6dd commit 6415b07
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/Authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ To avoid rendering a component and force waiting for the `authProvider` response

- `loading`: `true` just after mount, while the `authProvider` is being called. `false` once the `authProvider` has answered.
- `loaded`: the opposite of `loading`.
- `authenticated`: `undefined` while loading. then `true` or `false` depending on the `authProvider` response.
- `authenticated`: `true` while loading. then `true` or `false` depending on the `authProvider` response.

You can render different content depending on the authenticated status.

Expand Down
39 changes: 19 additions & 20 deletions packages/ra-core/src/auth/useAuthState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ interface State {
const emptyParams = {};

/**
* Hook for getting the authentication status and restricting access to authenticated users
* Hook for getting the authentication status
*
* Calls the authProvider.checkAuth() method asynchronously.
* If the authProvider returns a rejected promise, logs the user out.
*
* The return value updates according to the authProvider request state:
*
* - start: { authenticated: false, loading: true, loaded: false }
* - success: { authenticated: true, loading: false, loaded: true }
* - error: { authenticated: false, loading: false, loaded: true }
* - loading: true just after mount, while the authProvider is being called. false once the authProvider has answered.
* - loaded: the opposite of loading.
* - authenticated: true while loading. then true or false depending on the authProvider response.
*
* Useful in custom page components that can work both for connected and
* anonymous users. For pages that can only work for connected users,
* prefer the useAuthenticated() hook.
* To avoid rendering a component and force waiting for the authProvider response, use the useAuthState() hook
* instead of the useAuthenticated() hook.
*
* You can render different content depending on the authenticated status.
*
* @see useAuthenticated()
*
Expand All @@ -34,19 +34,18 @@ const emptyParams = {};
* @returns The current auth check state. Destructure as { authenticated, error, loading, loaded }.
*
* @example
* import { useAuthState } from 'react-admin';
* import { useAuthState, Loading } from 'react-admin';
*
* const CustomRoutes = [
* <Route path="/bar" render={() => {
* const { authenticated } = useAuthState({ myContext: 'foobar' });
* return authenticated ? <Bar /> : <BarNotAuthenticated />;
* }} />,
* ];
* const App = () => (
* <Admin customRoutes={customRoutes}>
* ...
* </Admin>
* );
* const MyPage = () => {
* const { loading, authenticated } = useAuthState();
* if (loading) {
* return <Loading />;
* }
* if (authenticated) {
* return <AuthenticatedContent />;
* }
* return <AnonymousContent />;
* };
*/
const useAuthState = (params: any = emptyParams): State => {
const [state, setState] = useSafeSetState({
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-core/src/auth/useCheckAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import useNotify from '../sideEffect/useNotify';
* and throws an error.
*
* This is a low level hook. See those more specialized hooks
* for common authentication tasks, based on useAuthCheck.
* for common authentication tasks, based on useCheckAuth.
*
* @see useAuthenticated
* @see useAuthState
Expand Down

0 comments on commit 6415b07

Please sign in to comment.