From 91446a239fe075b92e8a24c75c6611a16319a4e7 Mon Sep 17 00:00:00 2001 From: Brandon Bayer Date: Thu, 10 Jun 2021 16:39:42 -0400 Subject: [PATCH] fix Page.authenticate to never render if logged out (patch) --- packages/core/src/auth/auth-client.ts | 12 +++--- .../pages/page-dot-authenticate-logout.tsx | 36 +++++++++++++++++ .../auth/pages/page-dot-authenticate.tsx | 39 +++++++++++++++++++ test/integration/auth/test/index.test.ts | 9 +++++ 4 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 test/integration/auth/pages/page-dot-authenticate-logout.tsx create mode 100644 test/integration/auth/pages/page-dot-authenticate.tsx diff --git a/packages/core/src/auth/auth-client.ts b/packages/core/src/auth/auth-client.ts index 2a2bd54ec1..9b1ade8076 100644 --- a/packages/core/src/auth/auth-client.ts +++ b/packages/core/src/auth/auth-client.ts @@ -57,13 +57,11 @@ export const useAuthorize = () => { } export const useAuthorizeIf = (condition?: boolean) => { - useEffect(() => { - if (condition && !publicDataStore.getData().userId) { - const error = new AuthenticationError() - error.stack = null! - throw error - } - }) + if (typeof window !== "undefined" && condition && !publicDataStore.getData().userId) { + const error = new AuthenticationError() + error.stack = null! + throw error + } } export const useRedirectAuthenticated = (to: string) => { diff --git a/test/integration/auth/pages/page-dot-authenticate-logout.tsx b/test/integration/auth/pages/page-dot-authenticate-logout.tsx new file mode 100644 index 0000000000..d0f7b3b48c --- /dev/null +++ b/test/integration/auth/pages/page-dot-authenticate-logout.tsx @@ -0,0 +1,36 @@ +import logout from "app/mutations/logout" +import getAuthenticatedBasic from "app/queries/getAuthenticatedBasic" +import {BlitzPage, useMutation, useQuery} from "blitz" +import {Suspense} from "react" + +function Content() { + const [result] = useQuery(getAuthenticatedBasic, undefined) + const [logoutMutation] = useMutation(logout) + return ( +
+
{result}
+ +
+ ) +} + +const Page: BlitzPage = () => { + return ( +
+ + + +
+ ) +} + +Page.authenticate = {redirectTo: "/login"} + +export default Page diff --git a/test/integration/auth/pages/page-dot-authenticate.tsx b/test/integration/auth/pages/page-dot-authenticate.tsx new file mode 100644 index 0000000000..28a2e533fb --- /dev/null +++ b/test/integration/auth/pages/page-dot-authenticate.tsx @@ -0,0 +1,39 @@ +import logout from "app/mutations/logout" +import getAuthenticatedBasic from "app/queries/getAuthenticatedBasic" +import {BlitzPage, useMutation, useQuery} from "blitz" +import {Suspense} from "react" + +function Content() { + const [result] = useQuery(getAuthenticatedBasic, undefined) + const [logoutMutation] = useMutation(logout) + return ( +
+
{result}
+ +
+ ) +} + +const Page: BlitzPage = () => { + if (typeof window !== "undefined") { + throw new Error("This code should never run") + } + return ( +
+ + + +
+ ) +} + +Page.authenticate = true + +export default Page diff --git a/test/integration/auth/test/index.test.ts b/test/integration/auth/test/index.test.ts index 995499f958..d11368dcf3 100644 --- a/test/integration/auth/test/index.test.ts +++ b/test/integration/auth/test/index.test.ts @@ -29,6 +29,7 @@ describe("Auth", () => { "/login", "/noauth-query", "/authenticated-query", + "/page-dot-authenticate", "/api/queries/getNoauthBasic", "/api/queries/getAuthenticatedBasic", "/api/mutations/login", @@ -55,6 +56,14 @@ describe("Auth", () => { expect(text).toMatch(/AuthenticationError/) if (browser) await browser.close() }) + + it("should render error for protected page", async () => { + const browser = await webdriver(context.appPort, "/page-dot-authenticate") + await browser.waitForElementByCss("#error") + let text = await browser.elementByCss("#error").text() + expect(text).toMatch(/AuthenticationError/) + if (browser) await browser.close() + }) }) describe("authenticated", () => {