Preparing your workspace
Multi-tenant authorization uses the user's identity, organization membership, role, and resource ownership to determine database access.
Many applications need more than a simple user role. A user may belong to one or more organizations and have different permissions in each organization. A common design stores organization membership in a separate table containing organization ID, user ID, and role. RLS policies can then check whether auth.uid() belongs to the relevant organization and has the required role. This creates database-level tenant isolation and prevents one organization's data from being exposed to another.
An education organization platform can allow mentors to access assigned students, organization admins to manage members, and students to access only their own learning records.
create policy "Members can read organization courses"
on courses
for select
to authenticated
using (
exists (
select 1
from organization_members om
where om.organization_id = courses.organization_id
and om.user_id = auth.uid()
)
);The policy allows authenticated users to read courses only when they belong to the course's organization.