zhangyuang / egg-react-ssr

最小而美的Egg + React + SSR 服务端渲染应用骨架,同时支持JS和TS
http://doc.ssr-fc.com/
MIT License
1.91k stars 212 forks source link

example/ssr-with-ts实例cookie问题 #183

Closed zhangwei900808 closed 4 years ago

zhangwei900808 commented 4 years ago

在example/ssr-with-ts实例中,我设置cookie

  async index() {
    try {
      // Page为webpack打包的chunkName,项目默认的entry为Page
      this.ctx.type = 'text/html'
      this.ctx.status = 200
      this.ctx.cookies.set('isLogin', 'sdfdsf')
      this.ctx.apiService = this.service.index // 将service挂载到上下文对象
      const config = Object.assign(this.ctx.app.config, ssrConfig)
      setTimeout(() => {
     // 这里打印为undefined
        let isLogin = this.ctx.cookies.get('isLogin');
        console.log('islogin33', isLogin);
      }, 3000)
      const stream = await renderToStream(this.ctx, config)
      this.ctx.body = stream
    } catch (error) {
      this.ctx.logger.error(`Page Controller renderToStream Error`, error)
    }
  }

}

并且config.default.ts配置cookie

 config.cookies = {
    httpOnly: false,
    signed: false
  }

运行项目,打印结果显示undefined image

zhangwei900808 commented 4 years ago

如果设置signed:true,重新运行是好的,

 config.cookies = {
    httpOnly: false,
    signed: true
  }

image

zhangyuang commented 4 years ago

https://eggjs.org/zh-cn/core/cookie-and-session.html 请仔细阅读该文档。已经说的无比详细了。 image

zhangwei900808 commented 4 years ago

OK,全局设置了signed: false,获取的时候也要设置signed: false!

let isLogin = this.ctx.cookies.get('isLogin', {
                signed: false,
            });