如何在next.js中创建专用路由?

如何在next.js中创建专用路由?,next.js,next-router,Next.js,Next Router,我想包装我的整个应用程序,使其无法访问,除非用户登录。如果用户没有登录,我设法将用户重定向到登录页面,但是,在重定向发生之前,我仍然看到私有路由的闪光。如何避免这种情况?因为NextJS是服务器端呈现的,所以您需要使用getServerSideProps检查身份验证,或者在重定向之前在前端显示加载指示器 检查客户端的身份验证 创建一个包装器组件并将其放入\u app.js文件中。通过在用户仍在进行身份验证时显示加载组件,可以防止显示专用仪表板。注意:因为Next.js是服务器端呈现的,所以HTM

我想包装我的整个应用程序,使其无法访问,除非用户登录。如果用户没有登录,我设法将用户重定向到登录页面,但是,在重定向发生之前,我仍然看到私有路由的闪光。如何避免这种情况?

因为NextJS是服务器端呈现的,所以您需要使用
getServerSideProps
检查身份验证,或者在重定向之前在前端显示加载指示器

检查客户端的身份验证 创建一个包装器组件并将其放入
\u app.js
文件中。通过在用户仍在进行身份验证时显示加载组件,可以防止显示专用仪表板。注意:因为Next.js是服务器端呈现的,所以HTML总是在js重新水化之前显示。这意味着,第一次绘制总是在重定向开始之前进行

import { useRouter } from 'next/router'

export const AuthCheck = (props) => {
  const router = useRouter()
  const user = useUser() // you need to implement this. In this example, undefined means things are still loading, null means user is not signed in, anything truthy means they're signed in

  if (typeof window !== 'undefined' && user === null) router.push('/sign-in')

  if(!user) return <Loading /> // a loading component that prevents the page from rendering
   
  return props.children
}
检查服务器端的身份验证 假设您已经有了检查服务器端身份验证的代码设置,您可以使用此模式。注意:您需要将此添加到每个页面
getServerSideProps
\u app.js
\u document.js

const MyApp = ({ Component, pageProps }) => {
  return (
    <AuthCheck>
      <Component {...pageProps} />
    </AuthCheck>
  )
}

export default MyApp
export const getServerSideProps = async () => {
  const isAuthenticated = await checkAuthentication() // you need to implement this

  if (!isAuthenticated) {
    return {
      redirect: { destination: '/sign-in', permanent: false },
    }
  }
}

因为NextJS是服务器端呈现的,所以您需要使用
getServerSideProps
检查身份验证,或者在重定向之前在前端显示加载指示器

检查客户端的身份验证 创建一个包装器组件并将其放入
\u app.js
文件中。通过在用户仍在进行身份验证时显示加载组件,可以防止显示专用仪表板。注意:因为Next.js是服务器端呈现的,所以HTML总是在js重新水化之前显示。这意味着,第一次绘制总是在重定向开始之前进行

import { useRouter } from 'next/router'

export const AuthCheck = (props) => {
  const router = useRouter()
  const user = useUser() // you need to implement this. In this example, undefined means things are still loading, null means user is not signed in, anything truthy means they're signed in

  if (typeof window !== 'undefined' && user === null) router.push('/sign-in')

  if(!user) return <Loading /> // a loading component that prevents the page from rendering
   
  return props.children
}
检查服务器端的身份验证 假设您已经有了检查服务器端身份验证的代码设置,您可以使用此模式。注意:您需要将此添加到每个页面
getServerSideProps
\u app.js
\u document.js

const MyApp = ({ Component, pageProps }) => {
  return (
    <AuthCheck>
      <Component {...pageProps} />
    </AuthCheck>
  )
}

export default MyApp
export const getServerSideProps = async () => {
  const isAuthenticated = await checkAuthentication() // you need to implement this

  if (!isAuthenticated) {
    return {
      redirect: { destination: '/sign-in', permanent: false },
    }
  }
}

可能是重复的Yes@AndrewZheng我尝试过这种方法,但出于某种原因,我仍然看到它前面的私有页面闪烁redirects@poca我总是使用swr执行此操作,它会阻止渲染,直到满足条件为止。我使用swr对用户进行身份验证,并检查用户是否存在,然后我重定向可能重复的Yes@AndrewZheng。我尝试了这种方法,但出于某种原因,我仍然看到它前面的私有页面闪烁redirects@poca我总是使用swr执行此操作,它会阻止渲染,直到满足条件为止。我使用swr对该用户进行身份验证,并检查该用户是否存在,然后我重定向感谢Nick!,但是在实现这个之后,我得到了以下错误:'错误:没有找到路由器实例。您应该只在应用程序的客户端中使用“下一个/路由器”是的,忘记添加检查以查看代码是否在浏览器中运行。现在更新答案。谢谢Nick!,但是在实现这个之后,我得到了以下错误:'错误:没有找到路由器实例。您应该只在应用程序的客户端中使用“下一个/路由器”是的,忘记添加检查以查看代码是否在浏览器中运行。现在更新答案。