UNPKG

475 BJavaScriptView Raw
1import { Role } from "../constants";
2
3/**
4 * 中间件 计算动态角色
5 *
6 * @param {import("koa").Context} ctx koa context
7 * @param {import("koa").Next} next koa next
8 */
9export default async (ctx, next) => {
10 const { jwt, pet = {} } = ctx.state;
11 const { owner } = pet;
12 jwt.roles = jwt.roles || [];
13
14 // 注意这里 owner 是 mongo 的 objectId
15 if (owner == jwt.user /*eslint-disable-line*/) {
16 jwt.roles.push(Role.PET_STORE_OWNER);
17 }
18
19 return next();
20};