Most Express + Supabase tutorials start the same way: create one Supabase client at the top of the file with the service role key, and use it everywhere. It works on the first try, which is why so many projects do it.
It also quietly turns off the main security feature of Supabase. This post explains why, and shows the pattern I use instead: a small client created per request, using the caller’s own token.
The common setup
// supabase.js
import { createClient } from '@supabase/supabase-js';
export const supabase = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_SERVICE_ROLE_KEY
);
// a route
router.get('/todos', requireAuth, async (req, res) => {
const { data } = await supabase
.from('todos')
.select('*')
.eq('user_id', req.user.id); // you must remember this line, every time
res.json(data);
});
The service role key bypasses Row-Level Security completely. Your policies exist, but they never run for these queries.…