1
  • 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?
PirateApp
  • 5,433
  • 4
  • 57
  • 90
  • 1
    what kind of server are you using for `AdminJS`? You could create a little piece of middleware (express) or plugin (fastify) and quite literally just do `logger(req, res)`, you'll need the request context from adminjs, do you have some custom auth middleware? – bherbruck Jan 02 '23 at 03:11
  • @bherbruck @adminjs/express, i am using passport-local as well for email password login along with express-session, i would like to log the logged in user id on both admin and non admin routes – PirateApp Jan 02 '23 at 03:16
  • Which passport authentication scheme are you using and is it in a middleware or explicitly in individual route callbacks? If you have it as a middleware, you should be able to grab the context later in a logger middleware – bherbruck Jan 02 '23 at 03:26
  • @bherbruck i am using passport-local with express-session, the login and logout happens in explicit routes (updated the question) – PirateApp Jan 02 '23 at 03:27

0 Answers0