我在使用以下启动 repo 时遇到了一个问题:react-webpack-node
我正在使用sessionStorage进行身份验证,并意识到由于此启动器的服务器渲染性质,我无法在应用程序在服务器上渲染时访问此会话数据,即保护我的路线,我希望以下工作:
import React from 'react'
import { Route, IndexRoute } from 'react-router'
import App from 'containers/App'
import LoginPage from 'containers/LoginPage'
import DashboardPage from 'containers/DashboardPage'
export default (store) => {
const requireAuth = (nextState, replace, callback) => {
const token = sessionStorage.getItem('token')
if (!token) {
replace({
pathname: '/login',
state: { nextPathname: nextState.location.pathname }
})
}
callback()
}
const redirectAuth = (nextState, replace, callback) => {
const token = sessionStorage.getItem('token')
if (token) {
replace({
pathname: '/'
})
}
callback()
}
return (
<Route path='/' component={App}>
<IndexRoute component={DashboardPage} />
<Route path='/login' component={LoginPage} onEnter={redirectAuth} />
<Route path='/dashboard' component={DashboardPage} onEnter={requireAuth} />
<Route path='/logout' component={LoginPage} />
</Route>
)
}
但事实并非如此,我相信这是由于服务器上未定义会话。