express icon indicating copy to clipboard operation
express copied to clipboard

oidc does not exist on Request type

Open CalebJamesStevens opened this issue 1 year ago • 2 comments

Issue

import { Request } from 'express'

...

export async function getServerSideProps(context: GetServerSidePropsContext & { req: Request }) {
  const variable = context.req?.oidc?.user['parameter'];
}

Screenshot 2023-04-21 at 11 52 07 AM

Tried

  • Upgrading to 4.18.x
  • Upgrading to beta
  • Restarting ts server
  • re-yarn-ing

CalebJamesStevens avatar Apr 21 '23 18:04 CalebJamesStevens

are you using next.js? if yes you cannot mix express Request type with it

dtsuper3 avatar Apr 27 '23 20:04 dtsuper3

@CalebJamesStevens, like @dtsuper3 said, you can't mix an express and nextjs.

If you want to have oidc in the req object, you will need to modify the GetServerSidePropsContext type. Try doing this:

  • add a new next.d.ts file in your project and add the following code in it.
  • aim here is to add a new oidc property in it.
import type { GetServerSidePropsContext as OriginalGetServerSidePropsContext } from "next/types";

declare module "next" {
	export type GetServerSidePropsContext<
		Q extends ParsedUrlQuery = ParsedUrlQuery,
		D extends PreviewData = PreviewData,
	> = OriginalGetServerSidePropsContext<Q, D> & {
		req: {
			oidc: string; // use the correct type here
		};
	};

	export type GetServerSideProps<
		P extends { [key: string]: any } = { [key: string]: any },
		Q extends ParsedUrlQuery = ParsedUrlQuery,
		D extends PreviewData = PreviewData,
	> = (context: GetServerSidePropsContext<Q, D>) => Promise<GetServerSidePropsResult<P>>;
}

export {};

Aakash1103Jha avatar Apr 28 '23 07:04 Aakash1103Jha