22 lines
650 B
TypeScript
22 lines
650 B
TypeScript
import { STATUS_TOKEN_ERR, STATUS_WRONG_PARMS } from '@consts/statusCode';
|
|
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.exceptionResult(STATUS_WRONG_PARMS);
|
|
return;
|
|
}
|
|
const user = await UserModel.findUserByToken(ctx.request.body.token);
|
|
if (!user) {
|
|
console.error('token invalid');
|
|
ctx.body = ctx.service.utils.exceptionResult(STATUS_TOKEN_ERR);
|
|
return;
|
|
}
|
|
ctx.uid = user.uid;
|
|
await next();
|
|
};
|
|
};
|
|
|