const nocache = require('nocache');

module.exports = {
  init(server, node, opts) {
    server.get(`${opts.prefix}/api/did/session`, nocache(), async (req, res) => {
      if (!req.user) {
        res.json({ user: null });
        return;
      }

      const user = { ...req.user };
      if (user.role) {
        const teamDid = req.getBlockletDid();
        // FIXME: this code may have performance issue
        const rbac = await node.getRBAC(teamDid);
        user.permissions = await rbac.getScope(req.user.role);
      }

      res.json({ user });
    });
  },
};
