Skip to content

Commit

Permalink
fix pages to never render if Page.authenticate=true and logged out …
Browse files Browse the repository at this point in the history
…(patch) (#2476)
  • Loading branch information
flybayer authored Jun 10, 2021
1 parent 3138fc5 commit cac31cd
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 7 deletions.
12 changes: 5 additions & 7 deletions packages/core/src/auth/auth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
36 changes: 36 additions & 0 deletions test/integration/auth/pages/page-dot-authenticate-logout.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div>
<div id="content">{result}</div>
<button
id="logout"
onClick={async () => {
await logoutMutation()
}}
>
logout
</button>
</div>
)
}

const Page: BlitzPage = () => {
return (
<div id="page">
<Suspense fallback={"Loading..."}>
<Content />
</Suspense>
</div>
)
}

Page.authenticate = {redirectTo: "/login"}

export default Page
39 changes: 39 additions & 0 deletions test/integration/auth/pages/page-dot-authenticate.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div>
<div id="content">{result}</div>
<button
id="logout"
onClick={async () => {
await logoutMutation()
}}
>
logout
</button>
</div>
)
}

const Page: BlitzPage = () => {
if (typeof window !== "undefined") {
throw new Error("This code should never run")
}
return (
<div id="page">
<Suspense fallback={"Loading..."}>
<Content />
</Suspense>
</div>
)
}

Page.authenticate = true

export default Page
9 changes: 9 additions & 0 deletions test/integration/auth/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe("Auth", () => {
"/login",
"/noauth-query",
"/authenticated-query",
"/page-dot-authenticate",
"/api/queries/getNoauthBasic",
"/api/queries/getAuthenticatedBasic",
"/api/mutations/login",
Expand All @@ -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", () => {
Expand Down

0 comments on commit cac31cd

Please sign in to comment.