使用nodejs+redis+mysql开发忘记密码-设置用户密码和修改密码接口
创建忘记密码接口
http://127.0.0.1:8081/api/user/v1/forget
在roouter文件夹里创建接口地址
const express = require('express')
const router = express.Router()
const UserController = require('../controller/UserController')
router.post('/forget',UserController.forget)
module.exports = router
在app.js排除下接口
app.use(jwt({ secret: jwtSecretKey, algorithms: ['HS256'] }).unless({
path: [
/^\/api\/notify\/v1/,
/^\/api\/user\/v1\/register/,
/^\/api\/user\/v1\/forget/,
/^\/api\/wxLogin\/v1/,
]
}))
忘记密码控制层以及数据层逻辑开发
const UserService = require('../service/UserService.js')
const UserController = {
forget:async(req,res)=>{
let handleRes = await UserService.forget(req)
res.send(handleRes)
}
}
module.exports = UserController
const DB = require('../config/sequelize')
const redisConfig = require('../config/redisConfig')
const RandomTool = require('../utils/randomTool')
const SecretTool = require('../utils/SecretTool')
const BackCode = require('../utils/BackCode')
const codeEnum = require('../utils/CodeEnum')
const UserService = {
forget:async(req)=>{
let {phone,password,code} = req.body
let codeExit = await redisConfig.exists('change:code:'+phone)
if(!codeExit) return BackCode.buildError({msg:'请先获取手机验证码'})
let codeRes = await redisConfig.get('change:code:'+phone)
if(!(code==codeRes)) return BackCode.buildError({msg:'手机验证码输入不正确'})
let pwd = SecretTool.md5(password)
await DB.Account.update({pwd},{where:{phone}})
return BackCode.buildSuccessAndMsg({msg:'密码修改成功'})
}
}
module.exports = UserService
大功告成,可以在apifox上进行测试