How to integrate adminjs with pino-http?

195 Views Asked by At
  • I would like to log every request made into adminjs along with the account Id or admin user Id of the person who executed the request.
  • How can I hook into adminjs and log HTTP requests made by pino-http

This is what my authenticated router looks like

const adminbroRouter = AdminBroExpress.buildAuthenticatedRouter(
  adminBro,
  {
    authenticate: async (email, password) => {
      try {
        const account = await AuthService.getByEmail(email);
        if (!account) {
          return false;
        }
        const { password: accountPassword } =
          await AuthService.getPasswordByEmail(email);
        if (!(await isHashEqual(accountPassword, password))) {
          return false;
        }
        if (!AuthService.isAdmin(account as any)) {
          return false;
        }
        return account;
      } catch (error) {
        return false;
      }
    },
    cookiePassword: ADMINJS_COOKIE_PASSWORD,
    cookieName: ADMINJS_COOKIE_NAME,
    maxRetries: {
      count: ADMINJS_MAX_RETRIES_COUNT,
      duration: ADMINJS_MAX_RETRIES_DURATION,
    },
  },
  null,
  sessionOptions,
);

I am facing an issue with the customProps method in pino-http currently which is where I can get the accountId of the logged in user

customProps: (req, res) => {
  const accountId = req.user ? req.user.id : null;
  return {
    accountId,
  };
},

How do I get the admin user id in the above function or distinguish between when I am logged in as non-admin vs admin?

  • I am using passport-local with express-session
  • After reading through their docs, this is the closest thing I could find which is @adminjs/logger library
  • However it is not what I expected, any suggestions?
0

There are 0 best solutions below