8

是否有任何示例和/或方法可以在成功登录或注册凭据类型时重定向到私人仪表板next-auth?我找不到任何明确的文档。

我正在考虑在下面添加重定向,但不确定这是否是正确的方法:

callbacks.signIn = async (data, account, profile) => {
  if ((account.provider === 'google' && profile.verified_email === true) || (account.type === 'credentials' && data.status === 200)) {
    return Promise.resolve(true)
  } else {
    return Promise.resolve(false)
  }
}
4

2 回答 2

1

这实际上可以在启动登录时发生。从文档中,您可以将回调 URL 传递给登录。您的代码将如下所示。

  signIn(provider.id, {
      callbackUrl: `${window.location.origin}/protected`,
    })
于 2022-02-01T05:42:08.343 回答
0

使用新版本的 Next.js,您可以对“getStaticProps”方法进行重定向,如下所示,

资源:https ://nextjs.org/docs/basic-features/data-fetching#getstaticprops-static-generation

export async function getStaticProps(context) {
  // some imperative work..


  //
  if (!user) {
    return {
      redirect: {
        destination: '/', // some destination '/dashboard' Ex,
        permanent: false,
      },
    }
  }

  return {
    props: {},
  }
}
于 2020-11-27T11:56:29.340 回答