Reactjs 如何在React/Ant Design Pro中实现经过身份验证的路由
我希望能够执行:Reactjs 如何在React/Ant Design Pro中实现经过身份验证的路由,reactjs,authentication,react-router,antd,ant-design-pro,Reactjs,Authentication,React Router,Antd,Ant Design Pro,我希望能够执行: router.push('/compose') 如果没有登录,用户将被重定向到登录页面,并从那里转到/compose。 我很乐意学习通用的React解决方案或特定的Ant Design Pro解决方案。 我知道Droote和Design Pro已经授权,但我不知道它是否允许这样做 我正在使用react路由器4.3.1和antd 3.23.6 谢谢 编辑: 它可能需要像@tyler mcginnin那样: 函数privaterout({component:component,a
router.push('/compose')
如果没有登录,用户将被重定向到登录页面,并从那里转到/compose。
我很乐意学习通用的React解决方案或特定的Ant Design Pro解决方案。
我知道Droote和Design Pro已经授权,但我不知道它是否允许这样做
我正在使用react路由器4.3.1和antd 3.23.6
谢谢
编辑:
它可能需要像@tyler mcginnin那样:
函数privaterout({component:component,authored,…rest}){
返回(
授权===true
?
: }
/>
)
}
我使用React router,然后使用一个条件来实现这一点。如果我将一个isAuthenticated变量(设置为true或false)传递到props中,它将如下所示:
let routes = (//unauthenticated routes
<Switch>
<Route path = "/auth" component = {Auth} />
<Redirect to = "/auth" />
</Switch>
)
if(props.isAuthenticated){ //authenticated routes
routes = (
<Switch>
<Route path = "/manager" component = {DataManager} />
<Route path = "/logout" component = {Logout} />
<Redirect to = "/visualiser" />
</Switch>
)
}
// This sets up a <Redirect> component so that if the state isAuthenticated is true it will not show the /auth page and isntead will redirect to the desired component:
let authRedirect = null;
if (props.isAuthenticated) {
authRedirect = <Redirect to = "/visualiser" />
};
...
return(
<React.Fragment>
{/*The line below performs an auth redirect if the user is authenticated*/}
{authRedirect}
<h1>Welcome </h1>
</React.Fragment>
);
let routes=(//未经验证的路由
)
如果(props.isAuthenticated){//authenticated路由
路线=(
)
}
根据评论更新:
然后在auth组件中创建一个设置authredict变量的变量,然后在JSX中呈现该变量。大概是这样的:
let routes = (//unauthenticated routes
<Switch>
<Route path = "/auth" component = {Auth} />
<Redirect to = "/auth" />
</Switch>
)
if(props.isAuthenticated){ //authenticated routes
routes = (
<Switch>
<Route path = "/manager" component = {DataManager} />
<Route path = "/logout" component = {Logout} />
<Redirect to = "/visualiser" />
</Switch>
)
}
// This sets up a <Redirect> component so that if the state isAuthenticated is true it will not show the /auth page and isntead will redirect to the desired component:
let authRedirect = null;
if (props.isAuthenticated) {
authRedirect = <Redirect to = "/visualiser" />
};
...
return(
<React.Fragment>
{/*The line below performs an auth redirect if the user is authenticated*/}
{authRedirect}
<h1>Welcome </h1>
</React.Fragment>
);
//这将设置一个组件,以便如果“isAuthenticated”状态为true,它将不会显示/auth页面,并且isntead将重定向到所需的组件:
让authRedirect=null;
如果(道具已验证){
authRedirect=
};
...
返回(
{/*如果用户经过身份验证,下面的行将执行身份验证重定向*/}
{authRedirect}
欢迎
);
因此,基本上,您只需根据您的状态是否设置为已验证状态,有条件地返回authRedirect变量。我使用React router,然后使用条件返回。如果我将一个isAuthenticated变量(设置为true或false)传递到props中,它将如下所示:
let routes = (//unauthenticated routes
<Switch>
<Route path = "/auth" component = {Auth} />
<Redirect to = "/auth" />
</Switch>
)
if(props.isAuthenticated){ //authenticated routes
routes = (
<Switch>
<Route path = "/manager" component = {DataManager} />
<Route path = "/logout" component = {Logout} />
<Redirect to = "/visualiser" />
</Switch>
)
}
// This sets up a <Redirect> component so that if the state isAuthenticated is true it will not show the /auth page and isntead will redirect to the desired component:
let authRedirect = null;
if (props.isAuthenticated) {
authRedirect = <Redirect to = "/visualiser" />
};
...
return(
<React.Fragment>
{/*The line below performs an auth redirect if the user is authenticated*/}
{authRedirect}
<h1>Welcome </h1>
</React.Fragment>
);
let routes=(//未经验证的路由
)
如果(props.isAuthenticated){//authenticated路由
路线=(
)
}
根据评论更新:
然后在auth组件中创建一个设置authredict变量的变量,然后在JSX中呈现该变量。大概是这样的:
let routes = (//unauthenticated routes
<Switch>
<Route path = "/auth" component = {Auth} />
<Redirect to = "/auth" />
</Switch>
)
if(props.isAuthenticated){ //authenticated routes
routes = (
<Switch>
<Route path = "/manager" component = {DataManager} />
<Route path = "/logout" component = {Logout} />
<Redirect to = "/visualiser" />
</Switch>
)
}
// This sets up a <Redirect> component so that if the state isAuthenticated is true it will not show the /auth page and isntead will redirect to the desired component:
let authRedirect = null;
if (props.isAuthenticated) {
authRedirect = <Redirect to = "/visualiser" />
};
...
return(
<React.Fragment>
{/*The line below performs an auth redirect if the user is authenticated*/}
{authRedirect}
<h1>Welcome </h1>
</React.Fragment>
);
//这将设置一个组件,以便如果“isAuthenticated”状态为true,它将不会显示/auth页面,并且isntead将重定向到所需的组件:
让authRedirect=null;
如果(道具已验证){
authRedirect=
};
...
返回(
{/*如果用户经过身份验证,下面的行将执行身份验证重定向*/}
{authRedirect}
欢迎
);
因此,基本上,您只需根据您的状态是否设置为已验证状态有条件地返回authRedirect变量。我不想输入太多代码,但如果您需要更多上下文,请告诉我,我可以加强它。谢谢@Snek,我还需要它在loginHi@kambi之后重定向到目标组件,登录后,身份验证状态将发生更改,这将导致重新渲染。这意味着if(props.isAuthenticated)将再次运行,因此开关块底部的重定向将发生。这有帮助吗?嗨@Snek,谢谢,我知道重新登录后会显示路由器,但我希望它在登录后自动将我重定向到那里,我知道差距在哪里。我已经更新了我的答案。基本上,你只需要创建一个经过身份验证的视图,然后根据你的身份验证状态进行渲染。我不想在其中添加太多代码,但如果你需要更多的上下文,请告诉我,我可以增强它。谢谢@Snek,我还需要它在loginHi@kambi之后重定向到目标组件,登录后,身份验证状态将发生更改,这将导致重新渲染。这意味着if(props.isAuthenticated)将再次运行,因此开关块底部的重定向将发生。这有帮助吗?嗨@Snek,谢谢,我知道重新登录后会显示路由器,但我希望它在登录后自动将我重定向到那里,我知道差距在哪里。我已经更新了我的答案。基本上,您只需要创建一个经过身份验证的视图,然后根据您的身份验证状态呈现该视图。