自从 8.x 版本发布以来,我一直在使用 Node 6 和无服务器应用程序,我决定迁移到 async/await。
不过,我遇到了授权人功能的问题。由于我删除了回调参数并仅返回值,因此它停止工作。如果我向回调参数发送一些东西,它会继续正常工作,但它不像 async/await 那样。即使我抛出异常它也不起作用。
module.exports.handler = async (event, context) => {
if (typeof event.authorizationToken === 'undefined') {
throw new InternalServerError('Unauthorized');
}
const decodedToken = getDecodedToken(event.authorizationToken);
const isTokenValid = await validateToken(decodedToken);
if (!isTokenValid) {
throw new InternalServerError('Unauthorized');
} else {
return generatePolicy(decodedToken);
}
};
关于如何进行的任何建议?
谢谢大家!
最佳答案
我在这里遇到了同样的问题。似乎授权者还不支持异步/等待。一种解决方案是获取整个 async/await 函数并在处理程序中调用。像这样的:
const auth = async event => {
if (typeof event.authorizationToken === 'undefined') {
throw new InternalServerError('Unauthorized');
}
const decodedToken = getDecodedToken(event.authorizationToken);
const isTokenValid = await validateToken(decodedToken);
if (!isTokenValid) {
throw new InternalServerError('Unauthorized');
} else {
return generatePolicy(decodedToken);
}
}
module.exports.handler = (event, context, cb) => {
auth(event)
.then(result => {
cb(null, result);
})
.catch(err => {
cb(err);
})
};
关于async-await - 无服务器授权器异步等待不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50688624/