2017-06-20 73 views
1

我正在为我的前端使用Reactjs的Web应用程序。我阻止用户访问某些页面,例如他们已登录。我的问题是如何让用户访问他们预期的网址,而不是将他们重定向到我目前所做的主页。登录后将用户重定向到指定的网址Reactjs

我的路线是

<Switch> 
    <Route path="/desired-page" component={requireAuth(DesiredPage)} /> 
    <Route path="/new-page" component={requireAuth(NewPage)} /> 
</Switch> 

我requireAuth.js是

export default function(ComposedComponent) { 
    class Authenticate extends React.Component { 
    componentDidMount() { 
     if (!this.props.isAuthenticated) { 
     this.props.addFlashMessage({ 
      type: 'error', 
      text: 'You need to login to access this page' 
     }); 
     this.context.router.history.push('/login'); 
     } 
    } 
    render() { 
     return (
     <ComposedComponent {...this.props} /> 
    ) 
    } 
    } 

    Authenticate.propTypes = { 
    isAuthenticated: PropTypes.bool.isRequired, 
    addFlashMessage: PropTypes.func.isRequired 
    } 

    Authenticate.contextTypes = { 
    router:PropTypes.object.isRequired 
    } 

    function mapStateToProps(state) { 
    return { 
     isAuthenticated: state.auth.isAuthenticated 
    } 
    } 
    return connect(mapStateToProps, { addFlashMessage })(Authenticate); 
} 
+0

你的意思是他们登录后?你没有显示那部分代码。在内部'if(!this.props.isAuthenticated){'将URL保存为某种全局状态(例如,您的redux商店)。然后在成功登录后重定向到该页面。 –

+0

是的,他们登录后。我想到了这一点,但如何实施已成为一项挑战 – Dayvino

+0

将网址保存到全球商店并检索它一直是我面临的主要挑战。你能给一个指针吗? – Dayvino

回答

1

所以ReactTraining文档为您提供一个location道具代表,其中应用程序是现在,你希望它去的,或即使它在那里。

在导航到Login路线时,您会提及一个状态,您可以从哪条路线导航到登录。你可以做到这一点与

  • 重定向到
  • history.push
  • history.replace

要动态的路线,你可以用history.push通过它像

const location = { 
    pathname: '/login' 
    state: { from: 'Main' } 
} 

history.push(location) 

在你的情况将是

import {withRouter} from 'react-router'; 
export default function(ComposedComponent) { 
    class Authenticate extends React.Component { 
    componentDidMount() { 
     if (!this.props.isAuthenticated) { 
     this.props.addFlashMessage({ 
      type: 'error', 
      text: 'You need to login to access this page' 
     }); 
     const location = { 
      pathname: '/login' 
      state: { from: {pathname: '/Main'} } 
     } 
     this.props.history.push(location); 
     } 
    } 
    render() { 
     return (
     <ComposedComponent {...this.props} /> 
    ) 
    } 
    } 

    Authenticate.propTypes = { 
    isAuthenticated: PropTypes.bool.isRequired, 
    addFlashMessage: PropTypes.func.isRequired 
    } 

    Authenticate.contextTypes = { 
    router:PropTypes.object.isRequired 
    } 

    function mapStateToProps(state) { 
    return { 
     isAuthenticated: state.auth.isAuthenticated 
    } 
    } 
    return connect(mapStateToProps, { addFlashMessage })(withRouter(Authenticate)); 
} 

现在在重新引导回登录后,你可以用

var {from} = this.props.location.state || {from: {pathname: '/'}} 
this.props.history.push(from) 

注意访问此PARAM:如果你想使用history对象从道具让 一定要与withRouter来包装你的组件HOC。

我希望这有助于:)

+0

我试图实现,但我仍然得到相同的结果,即它将我重新导向回到主页,而我之前没有想要以用户身份访问的期望页面 – Dayvino

+0

@Dayvino,因此用户想要访问的页面将会从该页面本身重定向到用户登录,如果他没有登录。那么你是否设置了'state:{from:{pathname:'/ yourCurrentPageRoute'}}' –

+0

让我解释一下..我在主页上,当我发出产品页面的请求时,我被重定向到登录[如果我没有通过认证]以访问产品页面。当我登录时,我希望继续流向产品页面[因为有良好的用户体验],而不是回到我目前使用上述代码实现的主页。即使我尝试了你的代码,我仍然被重定向到主页,而不是我想要访问的期望页面(产品页面)。我是React的新手,所以原谅我的天真 – Dayvino