1

I am exploring making a T3 app (next/trpc/prisma) there the ~entire experience is behind login (Clerk for now).

All content in the DB is private, meaning that only the User's that are part of the same Group should have access to CRUD it.

Are there established patterns for making sure my trpc routers ensure the current user (via ctx, i guess?) can access the DB records being requested? I know there are protectProcedures, but I believe that just means the user has to be logged in - versus being logged in and having access to the records.

I had thought about exploring having discrete databases for each UserGroup - so a user only has access to a single DB, and that DB only contains its data - but that sounds difficult to auto-provision on new UseGroup signup (ill probably be using some cloud Postgres solution in the end).

empire29
  • 3,729
  • 6
  • 45
  • 71

1 Answers1

1

If you're using Postgres, you might want to have a look at RLS - it restricts which users can modify or access certain table rows, and is great for multi-tenancy. In your case, it could allow each user to have full access to rows within it's UserGroup, but not to rows from other groups.

Unfortunately, Prisma doesn't play too nicely on its own with RLS - however, there's an excellent library called ZenStack that provides an access control layer that might be what you're looking for, it's framework agnostic and works well with tRPC. In fact, from the ZenStack docs:

ZenStack makes things even easier by automatically generating tRPC routers from the ZModel schema. You can use the generated routers together with an enhanced Prisma client; since the Prisma client has the ability to enforce access policies, there is no need to implement authorization code anymore.

Hope this helps!