成功登录后重定向或在下次身份验证中注册凭据类型

Pas*_*per 6 javascript reactjs next.js next-auth

是否有任何示例和/或方法可以在成功登录或注册凭据类型后重定向到私人仪表板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)
  }
}
Run Code Online (Sandbox Code Playgroud)

Obe*_*bed 19

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

  signIn(provider.id, {
      callbackUrl: `${window.location.origin}/protected`,
    })
Run Code Online (Sandbox Code Playgroud)


小智 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: {},
  }
}
Run Code Online (Sandbox Code Playgroud)