I'm stuck writing an authentication router for a koa app.
I have a module that gets data from the DB then compares it to the request. I want to only run yield next
if the authentication passes.
The problem is that the module that communicates with the DB returns a promise and if I try to run yield next
inside that promise I get an error. Either SyntaxError: Unexpected strict mode reserved word
or SyntaxError: Unexpected identifier
depending on whether or not strict mode is used.
Here's a simplified example:
var authenticate = require('authenticate-signature');
// authRouter is an instance of koa-router
authRouter.get('*', function *(next) {
var auth = authenticate(this.req);
auth.then(function() {
yield next;
}, function() {
throw new Error('Authentication failed');
})
});