2017-06-20 68 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