(ctx, next)
| 7 | const { PUBLIC_KEY } = require('../app/config'); |
| 8 | |
| 9 | const verifyLogin = async (ctx, next) => { |
| 10 | console.log("验证登录的middleware~"); |
| 11 | |
| 12 | // 1.获取用户名和密码 |
| 13 | const { name, password } = ctx.request.body; |
| 14 | |
| 15 | // 2.判断用户名和密码是否为空 |
| 16 | if (!name || !password) { |
| 17 | const error = new Error(errorTypes.NAME_OR_PASSWORD_IS_REQUIRED); |
| 18 | return ctx.app.emit('error', error, ctx); |
| 19 | } |
| 20 | |
| 21 | // 3.判断用户是否存在的 |
| 22 | const result = await userService.getUserByName(name); |
| 23 | const user = result[0]; |
| 24 | if (!user) { |
| 25 | const error = new Error(errorTypes.USER_DOES_NOT_EXISTS); |
| 26 | return ctx.app.emit('error', error, ctx); |
| 27 | } |
| 28 | |
| 29 | // 4.判断密码是否和数据库中的密码是一致(加密) |
| 30 | if (md5password(password) !== user.password) { |
| 31 | const error = new Error(errorTypes.PASSWORD_IS_INCORRENT); |
| 32 | return ctx.app.emit('error', error, ctx); |
| 33 | } |
| 34 | |
| 35 | ctx.user = user; |
| 36 | await next(); |
| 37 | } |
| 38 | |
| 39 | const verifyAuth = async (ctx, next) => { |
| 40 | console.log("验证授权的middleware~"); |
nothing calls this directly
no test coverage detected