From d75555800982e07ebff310beb8df0c8906ced5f9 Mon Sep 17 00:00:00 2001 From: chenos Date: Fri, 18 Dec 2020 15:33:34 +0800 Subject: [PATCH] feat: add user check middleware --- packages/app/src/api/index.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/packages/app/src/api/index.ts b/packages/app/src/api/index.ts index 0f5d35087..991de5a46 100644 --- a/packages/app/src/api/index.ts +++ b/packages/app/src/api/index.ts @@ -37,6 +37,27 @@ const api = Api.create({ }, }); +api.resourcer.use(async (ctx: actions.Context, next) => { + const token = ctx.get('Authorization').replace(/^Bearer\s+/gi, ''); + // console.log('user check', ctx.action.params.actionName); + // const { actionName } = ctx.action.params; + if (!token) { + return next(); + } + const User = ctx.db.getModel('users'); + const user = await User.findOne({ + where: { + token, + }, + }); + if (!user) { + return next(); + } + ctx.state.currentUser = user; + // console.log('ctx.state.currentUser', ctx.state.currentUser); + await next(); +}); + api.resourcer.use(async (ctx: actions.Context, next) => { const { resourceName } = ctx.action.params; const table = ctx.db.getTable(resourceName); @@ -47,14 +68,22 @@ api.resourcer.use(async (ctx: actions.Context, next) => { } if (table) { const except = []; + const appends = []; for (const [name, field] of table.getFields()) { if (field.options.hidden) { except.push(field.options.name); } + if (field.options.appends) { + appends.push(field.options.name); + } } if (except.length) { ctx.action.setParam('fields.except', except); } + if (appends.length) { + ctx.action.setParam('fields.appends', appends); + } + console.log(ctx.action.params.fields); } await next(); });