import type { ParsedUrlQuery } from "querystring";
import { GetServerSideProps, GetServerSidePropsContext, GetServerSidePropsResult } from "next";
import { User } from "../../types/index.js";
import { Auth0Client } from "../client.js";
/**
* If you wrap your `getServerSideProps` with {@link WithPageAuthRequired} your props object will be augmented with
* the user property, which will be the {@link User} object.
*
* ```js
* // pages/profile.js
* import { auth0 } from "@/lib/auth0";
*
* export default function Profile({ user }) {
* return
Hello {user.name}
;
* }
*
* export const getServerSideProps = auth0.withPageAuthRequired();
* ```
*/
export type GetServerSidePropsResultWithSession = GetServerSidePropsResult
;
/**
* A page route that has been augmented with {@link WithPageAuthRequired}.
*/
export type PageRoute
= (ctx: GetServerSidePropsContext) => Promise>;
/**
* Objects containing the route parameters and search parameters of the page.
*/
export type AppRouterPageRouteOpts = {
params?: Promise>;
searchParams?: Promise<{
[key: string]: string | string[] | undefined;
}>;
};
/**
* An app route that has been augmented with {@link WithPageAuthRequired}.
* Returns any to be compatible with React's return types while avoiding React dependency.
*
* The generic parameter `P` allows passing Next.js `PageProps` or `LayoutProps`
* types for strongly-typed route parameters:
*
* ```ts
* export default auth0.withPageAuthRequired(
* async function Page(props: PageProps<"/customers/[id]/details">) {
* const { id } = await props.params;
* return {id}
;
* }
* );
* ```
*/
export type AppRouterPageRoute = (obj: P) => Promise;
/**
* If you have a custom returnTo url you should specify it in `returnTo`.
*
* You can pass in your own `getServerSideProps` method, the props returned from this will be
* merged with the user props. You can also access the user session data by calling `getSession`
* inside of this method. For example:
*
* ```js
* // pages/protected-page.js
* import { auth0 } from "@/lib/auth0";
*
* export default function ProtectedPage({ user, customProp }) {
* return Protected content
;
* }
*
* export const getServerSideProps = auth0.withPageAuthRequired({
* // returnTo: '/unauthorized',
* async getServerSideProps(ctx) {
* // access the user session if needed
* // const session = await auth0.getSession(ctx.req);
* return {
* props: {
* // customProp: 'bar',
* }
* };
* }
* });
* ```
*/
export type WithPageAuthRequiredPageRouterOptions = {
getServerSideProps?: GetServerSideProps
;
returnTo?: string;
};
/**
* Wrap your `getServerSideProps` with this method to make sure the user is authenticated before
* visiting the page.
*
* ```js
* // pages/protected-page.js
* import { auth0 } from "@/lib/auth0";
*
* export default function ProtectedPage() {
* return
Protected content
;
* }
*
* export const getServerSideProps = auth0.withPageAuthRequired();
* ```
*
* If the user visits `/protected-page` without a valid session, it will redirect the user to the
* login page. Then they will be returned to `/protected-page` after login.
*/
export type WithPageAuthRequiredPageRouter = (opts?: WithPageAuthRequiredPageRouterOptions
) => PageRoute
;
/**
* Specify the URL to `returnTo` - this is important in app router pages because the server component
* won't know the URL of the page.
* * @template P The type of the page or layout props, extending {@link AppRouterPageRouteOpts}.
* This allows the `returnTo` callback to access strongly-typed route parameters.
*/
export type WithPageAuthRequiredAppRouterOptions
= {
/**
* The URL to redirect the user to after a successful login.
* * Can be a static string or a function that receives the page props.
* When used as a function, the generic `P` ensures that `params` and `searchParams`
* match the specific types of your route (e.g., from Next.js `PageProps`).
*/
returnTo?: string | ((obj: P) => Promise | string);
};
/**
* Wrap your Server Component with this method to make sure the user is authenticated before
* visiting the page.
*
* ```js
* // app/protected-page/page.js
* import { auth0 } from "@/lib/auth0";
*
* const ProtectedPage = auth0.withPageAuthRequired(async function ProtectedPage() {
* return Protected content
;
* }, { returnTo: '/protected-page' });
*
* export default ProtectedPage;
* ```
*
* If the user visits `/protected-page` without a valid session, it will redirect the user to the
* login page.
*
* Note: Server Components are not aware of the req or the url of the page. So if you want the user to return to the
* page after login, you must specify the `returnTo` option.
*
* You can specify a function to `returnTo` that accepts the `params` (A Promise that resolves to
* an object containing the dynamic route parameters) and `searchParams` (A Promise that resolves to an
* object containing the search parameters of the current URL)
* argument from the page, to preserve dynamic routes and search params.
*
* ```js
* // app/protected-page/[slug]/page.js
* import { AppRouterPageRouteOpts } from '@auth0/nextjs-auth0/server';
* import { auth0 } from "@/lib/auth0";
*
* const ProtectedPage = auth0.withPageAuthRequired(async function ProtectedPage({
* params, searchParams
* }: AppRouterPageRouteOpts) {
* const slug = (await params)?.slug as string;
* return Protected content for {slug}
;
* }, {
* returnTo({ params }) {
* return `/protected-page/${(await params)?.slug}`;
* }
* });
*
* export default ProtectedPage;
* ```
*/
export type WithPageAuthRequiredAppRouter = (fn: AppRouterPageRoute
, opts?: WithPageAuthRequiredAppRouterOptions
) => AppRouterPageRoute
;
/**
* Protects Page router pages {@link WithPageAuthRequiredPageRouter} or
* App router pages {@link WithPageAuthRequiredAppRouter}
*/
export type WithPageAuthRequired = WithPageAuthRequiredPageRouter & WithPageAuthRequiredAppRouter;
export declare const appRouteHandlerFactory: (client: Auth0Client, config: {
loginUrl: string;
}) => WithPageAuthRequiredAppRouter;
export declare const pageRouteHandlerFactory: (client: Auth0Client, config: {
loginUrl: string;
}) => WithPageAuthRequiredPageRouter;