500 when logging in as new user in Production

System Information
  • Strapi Version: 4.4.0
  • Operating System: OSX
  • Database: Postgres
  • Node Version: 16.18.1
  • NPM Version: 8.19.2

Hi! When I create a new user in Production, and that user finishes their first login, their Admin UI just keeps spinning. The UI console looks like this:

<domain>/admin/users/me/permissions 500 (Internal server error)
<domain>/admin/information 500 (Internal server error)
<domain>/admin/users/me 500 (Internal server error)

I checked the backend logs as well, which state:

TypeError: Cannot read properties of undefined (reading 'handler')
0|<project>  |     at /home/ubuntu/<project>/node_modules/@strapi/permissions/lib/engine/index.js:90:86
0|<project>  |     at i (/home/ubuntu/<project>/node_modules/lodash/lodash.min.js:10:88)
0|<project>  |     at lf (/home/ubuntu/<project>/node_modules/lodash/lodash.min.js:84:172)
0|<project>  |     at l (/home/ubuntu/<project>/node_modules/lodash/lodash.min.js:58:213)
0|<project>  |     at runMicrotasks (<anonymous>)
0|<project>  |     at processTicksAndRejections (node:internal/process/task_queues:96:5)
0|<project>  |     at async evaluate (/home/ubuntu/<project>/node_modules/@strapi/permissions/lib/engine/index.js:107:35)
0|<project>  |     at async Object.generateAbility (/home/ubuntu/<project>/node_modules/@strapi/permissions/lib/engine/index.js:202:11)
0|<project>  |     at async Object.authenticate (/home/ubuntu/<project>/node_modules/@strapi/admin/server/strategies/admin.js:34:23)
0|<project>  |     at async Object.authenticate (/home/ubuntu/<project>/node_modules/@strapi/strapi/lib/services/auth/index.js:49:24)

I’ve made very few code-related modifications to the Strapi base, but I have created some custom RBAC conditions, which the above error seems to somewhat point towards. This is what my src/index.ts looks like:

type User = {
  id: number
  firstname: string
  lastname: string
  email: string
  isActive: boolean
  blocked: boolean
  username?: string
}

export default {
  /**
   * An asynchronous register function that runs before
   * your application is initialized.
   *
   * This gives you an opportunity to extend code.
   */
  register(/*{ strapi }*/) {},

  /**
   * An asynchronous bootstrap function that runs before
   * your application gets started.
   *
   * This gives you an opportunity to set up your data model,
   * run jobs, or perform some special logic.
   */
  async bootstrap({ strapi }) {
    await strapi.admin.services.permission.conditionProvider.register({
      displayName: 'Is in author list',
      name: 'is-in-author-list',
      plugin: 'admin',
      async handler(user: User) {
        return {
          Authors: { $elemMatch: { Email: user.email } },
        }
      },
    })

    await strapi.admin.services.permission.conditionProvider.register({
      displayName: 'Is in lecture creator list',
      name: 'is-in-lecture-creator-list',
      plugin: 'admin',
      async handler(user: User) {
        return {
          LectureCreators: { $elemMatch: { Email: user.email } },
        }
      },
    })

    await strapi.admin.services.permission.conditionProvider.register({
      displayName: 'Is in course creator list',
      name: 'is-in-course-creator-list',
      plugin: 'admin',
      async handler(user: User) {
        return {
          CourseCreators: { $elemMatch: { Email: user.email } },
        }
      },
    })
  },
}

When that was written, I tested the custom condition, and it worked. Nothing crashed. I can’t wrap my head around what makes this throw in Production when you login for the first time… Can someone please help me?

Edit: This does not happen locally when I do npm run buildnpm run start, create a new user and try to login with that user.

I found the issue now. The problem was that we on one role had set the Update access of a Course to ‘is in author list’. Course doesn’t have an Authors field, which the ‘is in author list’ custom condition implies.

To me it seems pretty extreme to have this small editorial miss crashing the whole admin panel when a user with a role with the above issue logs in. Does anyone have any advice on how to catch and handle this kind of error, in case it would happen again?

Thanks in advance