无服务器授权器异步等待不起作用

Kiw*_*nax 3 async-await serverless-framework

我一直在使用 Node 6 和我的无服务器应用程序,并且我决定迁移到 async/await,因为版本 8.x 已发布。

尽管如此,我的授权人功能有问题。由于我删除了回调参数并仅返回值,因此它停止工作。如果我向回调参数发送一些内容,它会继续正常工作,但它不是类似异步/等待的。即使我抛出异常,它也不起作用。

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);
    }
};
Run Code Online (Sandbox Code Playgroud)

有关如何进行的任何建议?

谢谢大家!

Tul*_*ria 6

我在这里遇到了同样的问题。授权者似乎还不支持 async/await。一种解决方案是获取整个 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);
      })
};
Run Code Online (Sandbox Code Playgroud)