Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[legacy-framework] fix pages to never render if Page.authenticate=true and logged out (patch) #2476

Merged
merged 1 commit into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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