我有部分应用程序(模块)将被禁止某些人,所以我想检查前钩住和发送未经授权的回应,如果需要的话。
我成功地在后端抛出错误,但在我的前端,我仍然得到成功的响应,好像没有错误一样。
1.检查发送请求的用户是否禁止应用的功能:
function isAppForbidden(hook) {
let forbiddenApps = [];
hook.app.services.settings.find({
query: {
$limit: 1,
$sort: {
createdAt: -1
}
}
}).then(res => {
let array = hook.params.user.hiddenApps;
if(array.indexOf('qualitydocs') >= 0 || res.data[0].forbiddenApps.indexOf('qualitydocs') >= 0) {
hook.response = Promise.reject({error: '401 Unauthorized'});
//this part is important, the rest not so much
//what im expecting to do here is just to return unauthorized response
}
});
return hook;
}
但现在这只会在后端引发错误,比如:
“错误:未经处理的拒绝时间:承诺承诺{
{错误:{code:'401',message:'Unauthorized'}}}code=401,message=Unauthorized”
前端仍然得到成功的响应(200个请求数据)
我只是在hooks之前调用这个函数:
before: {
all: [
authenticate('jwt'),
hook => includeBefore(hook),
hook => isAppForbidden(hook) //here, rest is not important
],
find: [],
get: [],
create: [(hook) => {
hook.data.authorId = hook.params.user.id;
}],
update: [],
patch: [],
remove: []
},