【问题标题】:Store / persist session during server rendering在服务器渲染期间存储/保持会话
【发布时间】:2016-07-04 08:22:24
【问题描述】:

我在使用以下启动 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>
  )
}

但事实并非如此,我相信这是由于服务器上未定义会话。

【问题讨论】:

    标签: javascript reactjs redux react-router server-rendering


    【解决方案1】:

    您已经指出了问题所在。您可以尝试使用 cookie 来存储令牌,因为 cookie 在客户端和服务器上都可以访问。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-26
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      • 2013-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多