code验证有问题
来源:8-8 注册&登录(1)

xxxxxbilibli
2018-10-31
无论验证码输什么 只要是4位 都能注册成功
代码都是复制源码的。
打印服务端返回的msg,返回‘注册成功’
解决方法:
这段注册成功的代码应该在判断code相等的情况下执行,不然无法判断验证码为空,或者验证码错误。
if (code) { const saveCode = await Store.hget(`nodemail:${username}`, 'code') const saveExpire = await Store.hget(`nodemail:${username}`, 'expire') if (code === saveCode) { if (new Date().getTime() - saveExpire > 0) { ctx.body = { code: -1, msg: '验证码已过期,请重新尝试' } return } // 判断注册成功与否应该放在这里 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!! let user = await User.find({username}) if (user.length) { ctx.body = { code: -1, msg: '已被注册' } return } let nuser = await User.create({username, password, email}) if (nuser) { let res = await axios.post('/users/signin', {username, password}) if (res.data && res.data.code === 0) { ctx.body = { code: 0, msg: '注册成功', user: res.data.user } } else { ctx.body = { code: -1, msg: 'error' } } } else { ctx.body = { code: -1, msg: '注册失败' } } } else { ctx.body = { code: -1, msg: '请填写正确的验证码' } } } else { ctx.body = { code: -1, msg: '请填写验证码' } }
写回答
2回答
-
Jam_Lee
2020-01-18
感谢楼主,这个问题困扰了我半天,总算解决了
00 -
快乐动起来呀
2018-10-31
每次验证码发送出去都会保存,验证的时候要去对比,你检查下存储的对吗,取出来的和存储的与发送的是否一致,调试下看看
022018-10-31
相似问题