Implementing User Roles

Hello, I am trying to implement roles of USER, MODERATOR, or ADMIN

I find the admin and organization plugins a bit intimidating
but more importantly I am not sure if they are the exact use case for my roles

here is my current code

export const auth = betterAuth({
  database: prismaAdapter(prisma, {
    provider: "postgresql",
  }),
  ...
  user: {
    additionalFields: {
      role: {
        type: "string",
        required: false,
        input: false,
      },
    },
  },
  databaseHooks: {
    user: {
      create: {
        before: async (user) => {
          const ADMIN_EMAILS = process.env.ADMIN_EMAILS?.split(";") || [];

          if (ADMIN_EMAILS.includes(user.email)) {
            return { data: { ...user, role: UserRole.ADMIN } };
          }

          const MODERATOR_EMAILS =
            process.env.MODERATOR_EMAILS?.split(";") || [];
          if (MODERATOR_EMAILS.includes(user.email)) {
            return { data: { ...user, role: UserRole.MODERATOR } };
          }

          return { data: user };
        },
      },
    },
  }
});


Is this a good approach? I have the default role as USER

Or should I be using the admin or organization plugins and adjusting them to fit my above requirements?
Was this page helpful?