DEV Community

Discussion on: Authorisation with Casbin and Koa Part 2

Collapse
 
liurongqing profile image
liurongqing
import { newEnforcer } from 'casbin';
const enforcer = await newEnforcer('path/to/model.conf', 'path/to/policy.csv');
app.use(authorisation(enforcer));
Enter fullscreen mode Exit fullscreen mode


js
Where does await need an async to be added?

Collapse
 
gerybbg profile image
Gergana Young

The function where you would initialise your koa app can be an async function. In my project what I do is I create my koa app and call the above two lines in an async function, then on start up I call that async function and only start the server inside the then. So something like this:

async function setup() {
  const app = new Koa();
  const enforcer = await newEnforcer('path/to/model.conf', 'path/to/policy.csv');
  app.use(authorisation(enforcer));

  //...all other setup

  return app;
}
Enter fullscreen mode Exit fullscreen mode

and then on start up of the service I would do something like this:

setup().then((app) => {
  app.listen(`<port number>`);
});
Enter fullscreen mode Exit fullscreen mode