Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

session 值获取失败 #3550

Closed
1 task
ChunrunHan opened this issue Mar 18, 2019 · 11 comments
Closed
1 task

session 值获取失败 #3550

ChunrunHan opened this issue Mar 18, 2019 · 11 comments

Comments

@ChunrunHan
Copy link

  • I have searched the issues of this repository and believe that this is not a duplicate.

Environment

Reproduction

第一步:用户获取验证码

// 1.router.js
  router.get('/captcha',controller.user.getCaptcha)
// 2.controller/user.js
  // 获取验证码
  async getCaptcha(){
    const {ctx} = this;
    var captcha = ctx.service.user.getCaptcha();
    ctx.session.code = captcha.text; // session 存储
    console.log('session 存储',ctx.session.code)
    ctx.body = captcha.data;
  }
//3.登录获取session.code undefined
  router.post('/login',controller.user.userLogin)
  // 登录
  async userLogin(){
    const {ctx} = this;
    console.log(ctx.request.body)
    let body = ctx.request.body;
    const { username, password, code } = ctx.request.body
    let resMsg = {
      code: 0,
      data: {},
      msg: '登录成功'
    }
    let isCaptchaVali = ctx.service.user.checkCaptcha(code)
    if (!isCaptchaVali) {
      resMsg.code = 1
      resMsg.msg = '验证码错误'
      ctx.body = resMsg
      return
    }
}

 // 检查验证码是否正确
  checkCaptcha(code){
    const {ctx} = this;
    code = code.toLowerCase()
    console.log("sessionCode",ctx.session.code)  // undefined?????????????
    let sessCode = ctx.session.code? ctx.session.code.toLowerCase():null
    console.log("sesscode",sessCode)
    //验证
    if(code === sessCode){
      // 成功之后验证码作废
      ctx.session.code = null
    }
    return code === sessCode;
  }
@ngot
Copy link
Member

ngot commented Mar 18, 2019

Duplicated with #3549

@egg-bot
Copy link

egg-bot commented Mar 18, 2019

Hello @ChunrunHan. Please provide a reproducible example following the instruction.

Issues labeled by Need Reproduce will be closed if no activities in 7 days.


@ChunrunHan,请根据这个说明提供最小可复现代码。

如果在 7 天内没有进展会被自动关闭。

@atian25
Copy link
Member

atian25 commented Mar 18, 2019

我试了下,在 home 里面是可以 print 出来的,自己检查下代码吧。

PS:你这不叫最小可复现了,一大堆干扰代码。另,你的秘钥都发出来了。

@ChunrunHan
Copy link
Author

To https://github.com/ChunrunHan/eggjs-server.git
8f4731e..683b395 dev -> dev
请切换到开发分支谢谢。

@atian25
Copy link
Member

atian25 commented Mar 18, 2019

一样的,你不要弄那么多复杂的,直接 npm init egg 初始化一个干净的项目,直接操作 ctx.session,在 controller.home.index 里面写入,在 controller.home.do 里面直接读取出来看看,我试了是没问题的。

@atian25
Copy link
Member

atian25 commented Mar 18, 2019

最小化可复现仓库的意思就是:从最干净的仓库开始复现问题,它能帮你排除掉不必要的干扰,往往在这过程中你就已经可以定位到问题,并快速解决了。

@ChunrunHan
Copy link
Author

我有个问题,egg解决跨域后,session 有冲突吗?

@atian25
Copy link
Member

atian25 commented Mar 18, 2019

我有个问题,egg解决跨域后,session 有冲突吗?

什么叫解决跨域后的 session 冲突?你就可以 egg 视为是一个 Web 服务器,按 HTTP 标准该怎么样就怎么样的。

@ChunrunHan
Copy link
Author

我现在安装完egg-cors 以后,session 就获取不到了。

@atian25
Copy link
Member

atian25 commented Mar 18, 2019

跨域肯定是拿不到 cookie 的。

请提供最小可复现方式。

@ImPigerla
Copy link

有种可能是:页面开了CDN缓存

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants