Menu

Supabase + Express: stop using the service role key for every request
📰

Supabase + Express: stop using the service role key for every request

DEV Community: express·SYED SHAAMEER Ali·about 23 hours ago
#uDzP4fxw
#devtoexpress#devto#dev#supabase#client
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.