Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.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
Javascript Next.js服务器端路由_Javascript_Reactjs_Authentication_Nextjs - Fatal编程技术网

Javascript Next.js服务器端路由

Javascript Next.js服务器端路由,javascript,reactjs,authentication,nextjs,Javascript,Reactjs,Authentication,Nextjs,我有这个代码来检查用户是否经过身份验证 const withAuth = AuthComponent => { class Authenticated extends Component { static async getInitialProps (ctx) { let componentProps if (AuthComponent.getInitialProps) { componentProps = await AuthCompo

我有这个代码来检查用户是否经过身份验证

const withAuth = AuthComponent => {
  class Authenticated extends Component {
    static async getInitialProps (ctx) {
      let componentProps
      if (AuthComponent.getInitialProps) {
        componentProps = await AuthComponent.getInitialProps(ctx)
      }
      return {
        ...componentProps
      }
    }
    componentDidMount () {
      this.props.dispatch(loggedIn())
    }
    renderProtectedPages (componentProps) {
      const { pathname } = this.props.url
      if (!this.props.isAuthenticated) {
        if (PROTECTED_URLS.indexOf(pathname) !== -1) {
          // this.props.url.replaceTo('/login')
          Router.replace('/login')                   // error
        }
      }
      return <AuthComponent {...componentProps} />
    }
    render () {
      const { checkingAuthState, ...componentProps } = this.props
      return (
        <div>
          {checkingAuthState ? (
            <div>
              <h2>Loading...</h2>
            </div>
          ) : (
            this.renderProtectedPages(componentProps)
          )}
        </div>
      )
    }
  }
  return connect(state => {
    const { checkingAuthState, isAuthenticated } = state.data.auth
    return {
      checkingAuthState,
      isAuthenticated
    }
  })(Authenticated)
}
constwithauth=AuthComponent=>{
类身份验证扩展组件{
静态异步getInitialProps(ctx){
让组件支持
if(AuthComponent.getInitialProps){
componentProps=等待AuthComponent.getInitialProps(ctx)
}
返回{
…组件和支柱
}
}
组件安装(){
this.props.dispatch(loggedIn())
}
renderProtectedPages(组件支柱){
const{pathname}=this.props.url
如果(!this.props.isAuthenticated){
如果(受保护的URL.indexOf(路径名)!=-1){
//this.props.url.replaceTo(“/login”)
Router.replace('/login')//错误
}
}
返回
}
渲染(){
const{checkingAuthState,…componentProps}=this.props
返回(
{检查授权状态(
加载。。。
) : (
此.renderProtectedPages(componentProps)
)}
)
}
}
返回连接(状态=>{
const{checkingAuthState,isAuthenticated}=state.data.auth
返回{
检查授权状态,
认证
}
})(认证)
}
它工作得很好,但我在尝试重定向用户时遇到了以下错误:

未找到路由器实例。您应该只在应用程序的客户端中使用“下一个/路由器”

如果我尝试使用
this.props.url.replaceTo('/login')
我会收到此警告

警告:“url.replaceTo()”已弃用。使用“下一个/路由器”API


所以这让我发疯了,我想知道是否有一种方法可以实现这种重定向,或者可能有另一种方法来控制身份验证在这种情况下,只要一个线索就好了。

我还想在服务器端找到解决方案。
但我通过编写“document.location=xxx”在客户机上修复了它。

您可能应该检查代码是在服务器端还是在客户机上执行的。 这样:

const isClient = typeof document !== 'undefined'
isClient && Router.replace('/login') 
要处理服务器端的重定向,只需使用服务器即可。 例如:

server.get("/super-secure-page", (req, res) => {
  // Use your own logic here to know if the user is loggedIn or not
  const token = req.cookies["x-access-token"]
  !token && res.redirect("/login")
  token && handle(req, res)
})
仅供参考,我从中得到了灵感