使用 getServerSideProps 重定向会返回一个错误。

huangapple go评论52阅读模式
英文:

Redirecting using getServerSideProps returns an error

问题

I'm providing the translated code snippet:

我试图在登录时重定向到/chat页面我正在使用auth0来处理身份验证不确定错误原因因为我在props中返回了空字符串然而错误不会影响网站重定向工作正常错误类型是主要问题我不太确定要在这里定义什么接口

export const getServerSideProps: GetServerSideProps = async (context) => {
  const { req, res } = context;
  const session = await getSession(req, res);

  if (session) {
    return {
      redirect: {
        destination: "/chat",
      },
    };
  }

  return {
    props: {},
  };
};

这是您提供的代码的翻译版本。如果您有其他问题或需要进一步帮助,请告诉我。

英文:

I'm trying to redirect to the /chat page when I'm logged in. I'm using auth0 to handle authentification. Not sure what the error is because I'm returning an empty string for props. The error however doesn't affect the website and redirection works fine. The wrong type is the main problem here. I'm not entirely sure what interface to define here.

export const getServerSideProps: GetServerSideProps = async (context) => {
  const { req, res } = context;
  const session = await getSession(req, res);

  if (session) {
    return {
      redirect: {
        destination: "/chat",
      },
    };
  }

  return {
    props: {},
  };
};

So far I've tried to go into GetServerSideProps types and adjusting context accordingly but it doesn't work. I get the following error:
> Type '(context: GetServerSidePropsContext<ParsedUrlQuery, PreviewData>) => Promise<{ redirect: { destination: string; }; props?: undefined; } | { ...; }>' is not assignable to type 'GetServerSideProps'.
Type 'Promise<{ redirect: { destination: string; }; props?: undefined; } | { props: {}; redirect?: undefined; }>' is not assignable to type 'Promise<GetServerSidePropsResult<{ [key: string]: any; }>>'.
Type '{ redirect: { destination: string; }; props?: undefined; } | { props: {}; redirect?: undefined; }' is not assignable to type 'GetServerSidePropsResult<{ [key: string]: any; }>'.
Type '{ redirect: { destination: string; }; props?: undefined; }' is not assignable to type 'GetServerSidePropsResult<{ [key: string]: any; }>'.
Type '{ redirect: { destination: string; }; props?: undefined; }' is not assignable to type '{ props: { [key: string]: any; } | Promise<{ [key: string]: any; }>; }'.
Types of property 'props' are incompatible.
Type 'undefined' is not assignable to type '{ [key: string]: any; } | Promise<{ [key: string]: any; }>'.ts(2322)

This is the code above getServerSideProps just in case:

import Head from &quot;next/head&quot;;
import Link from &quot;next/link&quot;;
import { useUser } from &quot;@auth0/nextjs-auth0/client&quot;;
import { getSession } from &quot;@auth0/nextjs-auth0&quot;;
import { GetServerSideProps } from &quot;next&quot;;

export default function Home() {
  const { isLoading, error, user } = useUser();

  if (isLoading) return &lt;div&gt;Loading...&lt;/div&gt;;
  if (error) return &lt;div&gt;{error.message}&lt;/div&gt;;

  return (
    &lt;div&gt;
      &lt;Head&gt;
        &lt;title&gt;Ask Me - Login or Signup&lt;/title&gt;
      &lt;/Head&gt;
      &lt;div&gt;
        &lt;div&gt;
          {user &amp;&amp; &lt;Link href=&quot;/api/auth/logout&quot;&gt;Logout&lt;/Link&gt;}
          {!user &amp;&amp; (
            &lt;&gt;
              &lt;Link
                href=&quot;/api/auth/login&quot;
              &gt;
                Login
              &lt;/Link&gt;
              &lt;Link
                href=&quot;/api/auth/signup&quot; &gt;
                Signup
              &lt;/Link&gt;
            &lt;/&gt;
          )}
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  );
}

答案1

得分: 2

你需要在重定向响应中包括 statusCode 属性。在这种情况下,您要使用临时重定向代码:

return {
  redirect: {
    destination: "/chat",
    statusCode: 302
  }
};
英文:

You have to include statusCode prop in your redirect response. In this case you want to use temporary redirect code:

return {
  redirect: {
    destination: &quot;/chat&quot;,
    statusCode: 302
  }
};

huangapple
  • 本文由 发表于 2023年5月22日 20:18:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定