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] Allow using Route Manifest for Page.redirectAuthenticatedTo #2243

Merged
merged 4 commits into from
May 17, 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
9 changes: 7 additions & 2 deletions packages/core/src/blitz-app-root.tsx
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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