Skip to content

Commit

Permalink
Add support for Page.redirectAuthenticatedTo to be a function with …
Browse files Browse the repository at this point in the history
…access to `session` (#2634)

(minor)
  • Loading branch information
s-r-x authored Aug 13, 2021
1 parent 595b294 commit 36c85a7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
22 changes: 13 additions & 9 deletions packages/core/src/blitz-app-root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,22 @@ export function withBlitzInnerWrapper(Page: BlitzPage) {
useAuthorizeIf(Page.authenticate === true)

if (typeof window !== "undefined") {
// We read directly from publicDataStore.getData().userId instead of useSession
const publicData = publicDataStore.getData()
// We read directly from publicData.userId instead of useSession
// so we can access userId on first render. useSession is always empty on first render
if (publicDataStore.getData().userId) {
if (publicData.userId) {
clientDebug("[BlitzInnerRoot] logged in")
let {redirectAuthenticatedTo} = Page
const redirectAuthenticatedTo =
typeof Page.redirectAuthenticatedTo === "function"
? Page.redirectAuthenticatedTo({session: publicData})
: Page.redirectAuthenticatedTo
if (redirectAuthenticatedTo) {
if (typeof redirectAuthenticatedTo !== "string") {
redirectAuthenticatedTo = formatWithValidation(redirectAuthenticatedTo)
}

clientDebug("[BlitzInnerRoot] redirecting to", redirectAuthenticatedTo)
const error = new RedirectError(redirectAuthenticatedTo)
const redirectUrl =
typeof redirectAuthenticatedTo === "string"
? redirectAuthenticatedTo
: formatWithValidation(redirectAuthenticatedTo)
clientDebug("[BlitzInnerRoot] redirecting to", redirectUrl)
const error = new RedirectError(redirectUrl)
error.stack = null!
throw error
}
Expand Down
12 changes: 11 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
NextPageContext,
} from "next/types"
import type {UrlObject} from "url"
import {PublicData} from "./auth/auth-types"
import {BlitzRuntimeData} from "./blitz-data"

export type {BlitzConfig} from "@blitzjs/config"
Expand Down Expand Up @@ -37,11 +38,20 @@ export type BlitzComponentType<C = NextPageContext, IP = {}, P = {}> = NextCompo
export interface AppProps<P = {}> extends NextAppProps<P> {
Component: BlitzComponentType<NextPageContext, any, P> & BlitzPage
}

export type RedirectAuthenticatedTo = string | RouteUrlObject | false
export type RedirectAuthenticatedToFnCtx = {
session: PublicData
}
export type RedirectAuthenticatedToFn = (
args: RedirectAuthenticatedToFnCtx,
) => RedirectAuthenticatedTo

export type BlitzPage<P = {}, IP = P> = NextPage<P, IP> & {
getLayout?: (component: JSX.Element) => JSX.Element
authenticate?: boolean | {redirectTo?: string | RouteUrlObject}
suppressFirstRenderFlicker?: boolean
redirectAuthenticatedTo?: string | RouteUrlObject
redirectAuthenticatedTo?: RedirectAuthenticatedTo | RedirectAuthenticatedToFn
}

export interface RouteUrlObject extends Pick<UrlObject, "pathname" | "query"> {
Expand Down

0 comments on commit 36c85a7

Please sign in to comment.