Skip to content

Commit

Permalink
Allow using Route Manifest for Page.redirectAuthenticatedTo (#2243)
Browse files Browse the repository at this point in the history
(minor)
  • Loading branch information
Skn0tt committed May 17, 2021
1 parent 79ca87e commit a535b7f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
9 changes: 7 additions & 2 deletions packages/core/src/blitz-app-root.tsx
@@ -1,3 +1,4 @@
import {formatWithValidation} from "next/dist/next-server/lib/utils"
import React, {ComponentPropsWithoutRef, useEffect} from "react"
import {useAuthorizeIf} from "./auth/auth-client"
import {publicDataStore} from "./auth/public-data-store"
Expand Down Expand Up @@ -58,8 +59,12 @@ export function withBlitzAppRoot(UserAppRoot: React.ComponentType<any>) {
const BlitzOuterRoot = (props: AppProps) => {
if (typeof window !== "undefined") {
if (publicDataStore.getData().userId) {
if (props.Component.redirectAuthenticatedTo) {
window.location.replace(props.Component.redirectAuthenticatedTo)
let {redirectAuthenticatedTo} = props.Component
if (redirectAuthenticatedTo) {
if (typeof redirectAuthenticatedTo !== "string") {
redirectAuthenticatedTo = formatWithValidation(redirectAuthenticatedTo)
}
window.location.replace(redirectAuthenticatedTo)
}
} else {
const authenticate = props.Component.authenticate
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/types.ts
Expand Up @@ -8,6 +8,7 @@ import {
NextPage,
NextPageContext,
} from "next/types"
import type {UrlObject} from "url"
import {BlitzRuntimeData} from "./blitz-data"

export type {
Expand Down Expand Up @@ -39,7 +40,11 @@ export type BlitzPage<P = {}, IP = P> = NextPage<P, IP> & {
getLayout?: (component: JSX.Element) => JSX.Element
authenticate?: boolean | {redirectTo?: string}
suppressFirstRenderFlicker?: boolean
redirectAuthenticatedTo?: string
redirectAuthenticatedTo?: string | RouteUrlObject
}

export interface RouteUrlObject extends Pick<UrlObject, "pathname" | "query"> {
pathname: string
}

export interface DefaultCtx {}
Expand Down
Expand Up @@ -98,12 +98,8 @@ exports.Routes = {
}
`.trim(),
declaration: `
import type { UrlObject } from "url"
import type { ParsedUrlQueryInput } from "querystring"
interface RouteUrlObject extends Pick<UrlObject, 'pathname' | 'query'> {
pathname: string
}
import type { RouteUrlObject } from "blitz"
export const Routes: {
Home(query?: ParsedUrlQueryInput): RouteUrlObject;
Expand Down
Expand Up @@ -80,12 +80,8 @@ export function generateManifest(
implementation:
"exports.Routes = {\n" + implementationLines.map((line) => " " + line).join(",\n") + "\n}",
declaration: `
import type { UrlObject } from "url"
import type { ParsedUrlQueryInput } from "querystring"
interface RouteUrlObject extends Pick<UrlObject, 'pathname' | 'query'> {
pathname: string
}
import type { RouteUrlObject } from "blitz"
export const Routes: {
${declarationLines.map((line) => " " + line).join(";\n")};
Expand Down

0 comments on commit a535b7f

Please sign in to comment.