2017-03-02 306 views
2

我正在使用redux-auth-wrapper來處理受保護路由的身份驗證路由邏輯。但是,這不是重定向。redux-auth-wrapper重定向不起作用

我的「用戶」的狀態對象是形狀的:

// state.auth 

{ 
    isAuthenticated: true, 
    isLoading: false, 
    loadError: null 
} 

這是我routes/index.js文件

const authenticated = UserAuthWrapper({ 
    authSelector: state => state.auth, 
    predicate: authData => authData.isAuthenticated, 
    redirectAction: routerActions.replace, 
    // redirects to /login by default 
}) 

export default (
    <Route path="/" component={App}> 
     <IndexRoute component={authenticated(Secret)} /> 
     <Route path="/login" component={Login} /> 
     <Route path="/item_1" component={Item1} /> 
     <Route path="/item_2" component={Item2}> 
      <Route path="/nested_item_2/:id" component={NestedItem2} /> 
     </Route> 
    </Route> 
) 

IndexRoute是一個受保護的組件,因此當我去localhost:3000,我期待它將我重定向到/login,但沒有任何反應。它只停留在具有空體的Secret組件上。

回答

2

得到了同樣的問題,並找出解決方案。您可能已經發現它一樣好,但這裏有雲:

你必須在你的代碼有這樣的重定向的工作:

import { createStore, applyMiddleware } from 'redux' 
import { browserHistory } from 'react-router' 
import { routerMiddleware } from 'react-router-redux' 

import rootReducer from './reducers.js' 

const middleware = routerMiddleware(browserHistory); 

const store = createStore(
    rootReducer, 
    preloadedState, 
    applyMiddleware(middleware) 
); 

傳遞給createStore()第三個參數是強制性

我找到了這個github topic的答案,請隨時查看更多的細節。 注意:Ctrl + F:「codereviewvideos」得到正確的答案;)

希望它有幫助!

+1

是的!這就是我所做的。感謝您提供我應該發佈的答案 – Carpetfizz