登录/退出react路由和expressjs路由



我想做的是有一个登录页面,一个注册页面。登录和注册工作很好,他们只是执行任何写在快递使用护照。然而,我想做的是创建一个路由'/dashboard'。我这样做了,它只是一个仪表板组件,由其他组件组成,比如导航条和body/content。我如何在react router中编写它,使它只允许我访问路由/仪表板,如果用户是经过身份验证的。在express中,我会这样写一个函数:

function isLoggedIn(req, res, next) {
   //if user is authenticated in the session, carry on
   if (req.isAuthenticated())
      return next(); //cuz we want to move on incase we're stuck here
   //if they arent redirect them to the home page
   res.redirect('/');
}

然而,我甚至没有在仪表板路线中写入express。我只在react-router中做过。我的组件层次结构现在是这样的

<App/>
  <Dashboard/>
     <NavBar/>
         <NavMenu/>
      <Body/>
<Login/>
<Signup/>
<About/>

我的路由是这样的:

<Route>
 <Route name ="signup" path="/signup" handler={Signup} />
 <Route name ="login" path="/" handler={Login} />
 <Route name ="app" path="/dashboard" handler={App} >
  <Route name ="logout" path="/logout" handler={NavMenu} />
 </Route>
</Route>

我不知道我是否掌握了正确的概念,但从网页显示的内容来看,它似乎不像。所以基本上在localhost:3000/出现了登录页面,我可以在登录页面和注册页面之间完全切换,当登录按钮被击中时,它使用express登录(它可以使用react-router来完成它,正确吗?),成功后它会转到/dashboard (res.redirect('/dashboard') in express)。react router处理的路由似乎也有单页应用的感觉,而express感觉就像我要去一个新的网页(我猜这是因为react-router只是改变了url并渲染了我们想要的组件?)在我的NavMenu组件中,我链接了logout按钮logout,但是url更改为localhost:3000/logout,什么也没发生

我将它嵌套在login下面,所以要访问仪表板和它的routehandler必须经过login。

<Login>
 <App>
   <RouteHandler>
     <dashboard../>
     <data../>
     <etc../>

最新更新