Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Next.js 将getServerSideProps函数放在助手中_Next.js_Getserversideprops - Fatal编程技术网

Next.js 将getServerSideProps函数放在助手中

Next.js 将getServerSideProps函数放在助手中,next.js,getserversideprops,Next.js,Getserversideprops,在这里 我想使用firebase管理方法保护许多页面: const getServerSideProps = async (ctx: GetServerSidePropsContext) => { try { const cookies = nookies.get(ctx); const token = await firebaseAdmin.auth().verifyIdToken(cookies.token); const { uid, email } =

在这里

我想使用firebase管理方法保护许多页面:

const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
  try {
    const cookies = nookies.get(ctx);
    const token = await firebaseAdmin.auth().verifyIdToken(cookies.token);
    const { uid, email } = token;

    return {
      props: {
        email,
        uid,
      },
    };
  } catch (err) {
    return {
      redirect: {
        permanent: false,
        destination: "/",
      },
      props: {} as unknown as never,
    };
  }
};

const ExpertPage = (
  props: InferGetServerSidePropsType<typeof getServerSideProps>,
) => {
  return (
    <Layout type={ILayoutType.EXPERT}>
      <ProtectedPage1 props={props} />
    </Layout>
  );
};

export default ExpertPage;
const getServerSideProps=async(ctx:GetServerSidePropsContext)=>{
试一试{
const cookies=nookies.get(ctx);
const token=wait firebaseAdmin.auth().verifyIdToken(cookies.token);
const{uid,email}=token;
返回{
道具:{
电子邮件,
uid,
},
};
}捕捉(错误){
返回{
重定向:{
永久性:假,
目的地:“/”,
},
道具:{}从未有过的未知,
};
}
};
const ExpertPage=(
props:InferGetServerSidePropsType,
) => {
返回(
);
};
导出默认ExpertPage;
如您所见,应该在所有这些页面上调用函数
getServerSideProps
,这是我想要避免的

我试图在助手中移动它,以便在此处仅将其作为类型调用
InferGetServerSidePropsType


但它没有运行。我想知道是否有更好的方法来做这件事

这是否有助于回答您的问题:?您可以创建一个包装器函数,在所有页面的
getServerSideProps
函数上调用它。