diff --git a/.gitignore b/.gitignore index cbed8056b..25e502909 100644 --- a/.gitignore +++ b/.gitignore @@ -61,7 +61,7 @@ typings/ .yarn-integrity # dotenv environment variables file -.env +.env.local # Distribution directories dist/ diff --git a/EXAMPLES.md b/EXAMPLES.md index 249f57385..e4d0bc752 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -61,9 +61,9 @@ Check the user's authentication state and log them in or out from the front end import { useUser } from '@auth0/nextjs-auth0'; export default () => { - const { user, loading } = useUser(); + const { user, isLoading } = useUser(); - if (loading) return
Loading...
; + if (isLoading) return
Loading...
; if (user) { return ( diff --git a/README.md b/README.md index cbde970aa..8dd936ffb 100644 --- a/README.md +++ b/README.md @@ -104,9 +104,9 @@ Check whether a user is authenticated by checking that `user` has a value, and l import { useUser } from '@auth0/nextjs-auth0'; export default () => { - const { user, loading } = useUser(); + const { user, isLoading } = useUser(); - if (loading) return
Loading...
; + if (isLoading) return
Loading...
; if (user) { return ( diff --git a/examples/basic-example/.env.template b/examples/basic-example/.env.local.template similarity index 100% rename from examples/basic-example/.env.template rename to examples/basic-example/.env.local.template diff --git a/examples/basic-example/components/header.jsx b/examples/basic-example/components/header.jsx index 1ea96a614..973b36b74 100644 --- a/examples/basic-example/components/header.jsx +++ b/examples/basic-example/components/header.jsx @@ -3,7 +3,7 @@ import Link from 'next/link'; import { useUser } from '@auth0/nextjs-auth0'; const Header = () => { - const { user, loading } = useUser(); + const { user, isLoading } = useUser(); return (
@@ -19,7 +19,7 @@ const Header = () => { Protected Page - {!loading && + {!isLoading && (user ? ( <>
  • diff --git a/examples/basic-example/pages/index.jsx b/examples/basic-example/pages/index.jsx index ff2a4272c..e3940e651 100644 --- a/examples/basic-example/pages/index.jsx +++ b/examples/basic-example/pages/index.jsx @@ -4,15 +4,15 @@ import { useUser } from '@auth0/nextjs-auth0'; import Layout from '../components/layout'; export default function Home() { - const { user, loading } = useUser(); + const { user, isLoading } = useUser(); return (

    Next.js and Auth0 Example

    - {loading &&

    Loading login info...

    } + {isLoading &&

    Loading login info...

    } - {!loading && !user && ( + {!isLoading && !user && ( <>

    To test the login click in Login diff --git a/examples/basic-example/pages/protected-page.jsx b/examples/basic-example/pages/protected-page.jsx index 505e36605..1b50251b7 100644 --- a/examples/basic-example/pages/protected-page.jsx +++ b/examples/basic-example/pages/protected-page.jsx @@ -4,15 +4,15 @@ import { useUser, withPageAuthRequired } from '@auth0/nextjs-auth0'; import Layout from '../components/layout'; export default function ProtectedPage() { - const { user, loading } = useUser(); + const { user, isLoading } = useUser(); return (

    Protected Page

    - {loading &&

    Loading profile...

    } + {isLoading &&

    Loading profile...

    } - {!loading && user && ( + {!isLoading && user && ( <>

    Profile:

    {JSON.stringify(user, null, 2)}
    diff --git a/examples/kitchen-sink-example/.env.template b/examples/kitchen-sink-example/.env.local.template similarity index 100% rename from examples/kitchen-sink-example/.env.template rename to examples/kitchen-sink-example/.env.local.template diff --git a/examples/kitchen-sink-example/components/header.tsx b/examples/kitchen-sink-example/components/header.tsx index 46e4170cf..78e4f2502 100644 --- a/examples/kitchen-sink-example/components/header.tsx +++ b/examples/kitchen-sink-example/components/header.tsx @@ -3,7 +3,7 @@ import Link from 'next/link'; import { useUser } from '@auth0/nextjs-auth0'; const Header: React.FunctionComponent = () => { - const { user, loading } = useUser(); + const { user, isLoading } = useUser(); return (
    @@ -24,7 +24,7 @@ const Header: React.FunctionComponent = () => { TV Shows
  • - {!loading && + {!isLoading && (user ? ( <>
  • diff --git a/examples/kitchen-sink-example/pages/index.tsx b/examples/kitchen-sink-example/pages/index.tsx index a6a4ed72e..eee707566 100644 --- a/examples/kitchen-sink-example/pages/index.tsx +++ b/examples/kitchen-sink-example/pages/index.tsx @@ -4,15 +4,15 @@ import { useUser } from '@auth0/nextjs-auth0'; import Layout from '../components/layout'; export default function Home(): React.ReactElement { - const { user, loading } = useUser(); + const { user, isLoading } = useUser(); return (

    Next.js and Auth0 Example

    - {loading &&

    Loading login info...

    } + {isLoading &&

    Loading login info...

    } - {!loading && !user && ( + {!isLoading && !user && ( <>

    To test the login click in Login diff --git a/examples/kitchen-sink-example/pages/profile.tsx b/examples/kitchen-sink-example/pages/profile.tsx index 0e045327f..d01966d6f 100644 --- a/examples/kitchen-sink-example/pages/profile.tsx +++ b/examples/kitchen-sink-example/pages/profile.tsx @@ -4,15 +4,15 @@ import { useUser, withPageAuthRequired } from '@auth0/nextjs-auth0'; import Layout from '../components/layout'; export default withPageAuthRequired(function Profile(): React.ReactElement { - const { user, loading } = useUser(); + const { user, isLoading } = useUser(); return (

    Profile

    - {loading &&

    Loading profile...

    } + {isLoading &&

    Loading profile...

    } - {!loading && user && ( + {!isLoading && user && ( <>

    Profile:

    {JSON.stringify(user, null, 2)}
    diff --git a/package.json b/package.json index 670ed8589..e32f042be 100644 --- a/package.json +++ b/package.json @@ -17,26 +17,26 @@ }, "scripts": { "clean": "rimraf dist", - "pretty": "prettier --write \"src/**/*.ts\" \"src/*.ts\"", + "pretty": "prettier --write \"src/**/*.{ts,tsx}\" \"src/*.ts\"", "lint": "eslint --fix --ext .ts ./src", - "build": "tsc -p tsconfig.build.json", - "build:test": "next build tests/fixtures/test-app", - "build:kitchen-sync": "npm run build --prefix=examples/kitchen-sync-example", - "build:examples": "npm run build:kitchen-sync", - "test": "jest tests --coverage --maxWorkers=10", - "test:watch": "jest --coverage --watch", - "prepublishOnly": "npm test && npm run lint", - "prepublish": "npm run build", "docs": "typedoc --options typedoc.js src", + "prepublish": "npm run build", + "prepublishOnly": "npm test && npm run lint", "install:examples": "npm i --prefix=examples/basic-example --no-package-lock && npm i --prefix=examples/kitchen-sink-example --no-package-lock", + "build": "tsc -p tsconfig.build.json", + "build:test": "next build tests/fixtures/test-app", + "build:kitchen-sink": "npm run build --prefix=examples/kitchen-sink-example", + "build:examples": "npm run build:kitchen-sink", + "build:vercel": "npm run install:examples && npm run build && npm run build:examples", "start:basic": "npm run dev --prefix=examples/basic-example", "start:kitchen-sink": "npm run dev --prefix=examples/kitchen-sink-example", + "test": "jest tests --coverage --maxWorkers=10", + "test:watch": "jest --coverage --watch", "test:kitchen-sink": "start-server-and-test start:kitchen-sink http-get://localhost:3000 cypress:run", "test:kitchen-sink:watch": "start-server-and-test start:kitchen-sink 3000 cypress:open", "test:integration": "npm run test:kitchen-sink", "cypress:run": "cypress run", - "cypress:open": "cypress open", - "build:vercel": "npm run install:examples && npm run build && npm run build:examples" + "cypress:open": "cypress open" }, "repository": { "type": "git", diff --git a/src/frontend/use-user.tsx b/src/frontend/use-user.tsx index b40a3ce81..1d29b7711 100644 --- a/src/frontend/use-user.tsx +++ b/src/frontend/use-user.tsx @@ -23,7 +23,7 @@ export interface UserProfile { */ export interface UserContext { user?: UserProfile; - loading: boolean; + isLoading: boolean; } /** @@ -63,7 +63,7 @@ type UserProviderProps = React.PropsWithChildren<{ user?: UserProfile; profileUr /** * @ignore */ -const User = createContext({ loading: false }); +const User = createContext({ isLoading: false }); /** * The `useUser` hook, which will get you the {@link UserProfile} object from the server side session by requesting it @@ -75,9 +75,9 @@ const User = createContext({ loading: false }); * import { useUser } from '@auth0/nextjs-auth0`; * * export default function Profile() { - * const { loading, user } = useUser(); + * const { user, isLoading } = useUser(); * - * if (loading) return
    Loading...
    ; + * if (isLoading) return
    Loading...
    ; * if (!user) return Login; * return
    Hello {user.name}, Logout
    ; * } @@ -105,7 +105,7 @@ export default ({ profileUrl = '/api/auth/me' }: UserProviderProps): ReactElement => { const [user, setUser] = useState(() => initialUser); - const [loading, setLoading] = useState(() => !initialUser); + const [isLoading, setIsLoading] = useState(() => !initialUser); useEffect((): void => { if (user) return; @@ -115,9 +115,9 @@ export default ({ const result = response.ok ? await response.json() : undefined; setUser(result); - setLoading(false); + setIsLoading(false); })(); }, [user]); - return {children}; + return {children}; }; diff --git a/src/frontend/with-page-auth-required.tsx b/src/frontend/with-page-auth-required.tsx index 58962c3e5..e2b9b6052 100644 --- a/src/frontend/with-page-auth-required.tsx +++ b/src/frontend/with-page-auth-required.tsx @@ -67,15 +67,15 @@ const withPageAuthRequired: WithPageAuthRequired = (Component, options = {}) => return function withPageAuthRequired(props): JSX.Element { const router = useRouter(); const { returnTo = router.asPath, onRedirecting = defaultOnRedirecting, loginUrl = '/api/auth/login' } = options; - const { user, loading } = useUser(); + const { user, isLoading } = useUser(); useEffect(() => { - if (user || loading) return; + if (user || isLoading) return; (async (): Promise => { await router.push(`${loginUrl}?returnTo=${returnTo}`); })(); - }, [user, loading, router, loginUrl, returnTo]); + }, [user, isLoading, router, loginUrl, returnTo]); return user ? : onRedirecting(); }; diff --git a/tests/frontend/use-user.test.tsx b/tests/frontend/use-user.test.tsx index b566f1e5f..618170394 100644 --- a/tests/frontend/use-user.test.tsx +++ b/tests/frontend/use-user.test.tsx @@ -8,7 +8,7 @@ describe('context wrapper', () => { const { result } = renderHook(() => useUser(), { wrapper: withUser(user) }); expect(result.current.user).toEqual(user); - expect(result.current.loading).toEqual(false); + expect(result.current.isLoading).toEqual(false); }); test('should fetch the user', async () => { @@ -17,12 +17,12 @@ describe('context wrapper', () => { const { result, waitForValueToChange } = renderHook(() => useUser(), { wrapper: withUser() }); expect(result.current.user).toEqual(undefined); - expect(result.current.loading).toEqual(true); + expect(result.current.isLoading).toEqual(true); - await waitForValueToChange(() => result.current.loading); + await waitForValueToChange(() => result.current.isLoading); expect(result.current.user).toEqual(user); - expect(result.current.loading).toEqual(false); + expect(result.current.isLoading).toEqual(false); }); test('should fail to fetch the user', async () => { @@ -31,12 +31,12 @@ describe('context wrapper', () => { const { result, waitForValueToChange } = renderHook(() => useUser(), { wrapper: withUser() }); expect(result.current.user).toEqual(undefined); - expect(result.current.loading).toEqual(true); + expect(result.current.isLoading).toEqual(true); - await waitForValueToChange(() => result.current.loading); + await waitForValueToChange(() => result.current.isLoading); expect(result.current.user).toEqual(undefined); - expect(result.current.loading).toEqual(false); + expect(result.current.isLoading).toEqual(false); }); afterAll(() => {