import { GetServerSideProps , GetServerSidePropsContext , GetServerSidePropsResult } from 'next' ; import { Claims , GetSession } from '../session' ; import { assertCtx } from '../utils/assert' ; import React , { ComponentType } from 'react' ; import { UserProps , WithPageAuthRequiredOptions as WithPageAuthRequiredCSROptions , WithPageAuthRequiredProps } from '../frontend/with-page-auth-required' ; import { withPageAuthRequired as withPageAuthRequiredCSR } from '../frontend' ; import { ParsedUrlQuery } from 'querystring' ; /**
 * If you wrap your `getServerSideProps` with  WithPageAuthRequired your props object will be augmented with
 * the user property, which will be the user's  Claims *
 * ```js
 * // pages/profile.js
 * import { withPageAuthRequired } from '@auth0/nextjs-auth0';
 *
 * export default function Profile({ user }) {
 *   return <div>Hello {user.name}</div>;
 * }
 *
 * export const getServerSideProps = withPageAuthRequired();
 * ```
 *
 *  category export type GetServerSidePropsResultWithSession < P = any > = GetServerSidePropsResult < P & { user ? : Claims | null ; } > ; /**
 * A page route that has been augmented with  WithPageAuthRequired category export type PageRoute < P , Q extends ParsedUrlQuery > = ( cts : GetServerSidePropsContext < Q > ) => Promise < GetServerSidePropsResultWithSession < P > > ; category export type WithPageAuthRequiredOptions < P = any , Q extends ParsedUrlQuery = ParsedUrlQuery > = { getServerSideProps ? : GetServerSideProps < P , Q > ; returnTo ? : string ; } ; category export type WithPageAuthRequired = { < P extends WithPageAuthRequiredProps > ( Component , options ? ) : React . FC < P > ; < P , Q extends ParsedUrlQuery = ParsedUrlQuery > ( opts ? ) : PageRoute < P , Q > ; } ; ignore export default function withPageAuthRequiredFactory ( loginUrl , getSession ) { return ( optsOrComponent = { } , csrOpts ? ) : any => { if ( typeof optsOrComponent === 'function' ) { return withPageAuthRequiredCSR ( optsOrComponent , csrOpts ) ; } const { getServerSideProps , returnTo } = optsOrComponent ; return async ( ctx ) : Promise < GetServerSidePropsResultWithSession > => { assertCtx ( ctx ) ; const session = getSession ( ctx . req , ctx . res ) ; if ( ! session ?. user ) { return { redirect : { destination : `${ loginUrl }?returnTo=${ encodeURIComponent ( returnTo || ctx . resolvedUrl ) }` , permanent : false } } ; } let ret = { props : { } } ; if ( getServerSideProps ) { ret = await getServerSideProps ( ctx ) ; } return { ... ret , props : { ... ret . props , user : session . user } } ; } ; } ; }