26 lines
752 B
TypeScript
26 lines
752 B
TypeScript
import { STATUS } from '@consts';
|
|
import { UserModel } from '@db/User';
|
|
|
|
module.exports = () => {
|
|
return async function tokenParser(ctx, next) {
|
|
if (!ctx.request.body || !ctx.request.body.token) {
|
|
console.error('token not found');
|
|
ctx.body = ctx.service.utils.resResult(STATUS.WRONG_PARMS);
|
|
return;
|
|
}
|
|
const user = await UserModel.findUserByToken(ctx.request.body.token);
|
|
if (!user) {
|
|
console.error('token invalid');
|
|
ctx.body = ctx.service.utils.resResult(STATUS.TOKEN_ERR);
|
|
return;
|
|
}
|
|
ctx.uid = user.uid;
|
|
ctx.serverType = ctx.request.body.serverType||user.serverType;
|
|
ctx.auth = user.auth;
|
|
ctx.userCode = user.userCode;
|
|
ctx.pkgName = user.pkgName;
|
|
await next();
|
|
};
|
|
};
|
|
|