Open2

next.jsとsupabaseでRow Level Securityを使用して権限による抽出をおこなってみた

kazukazu

サンプルのテーブル
create table
public.posts (
id serial,
title text null,
created_at timestamp with time zone null default now(),
user_id uuid null,
content text null,
constraint posts_pkey primary key (id)
) tablespace pg_default;

実際にはログインした後に認証後のロウレベルsecurityは下記になる

ログインかつ、管理者(プロファイルのintroduceがadminの場合

(( SELECT profiles.introduce
FROM profiles
WHERE (profiles.id = auth.uid())) = 'admin'::text)

ログインかつというのはオーソリケーデットがついているかつ、ユーザーテーブル(オーステーブルと連携している)テーブルをauth.uidと比較して、権限(ロールをもってくる)合致すれば全件取得

さらに個別抽出としてはべつなsecurityをつくり、

(auth.uid() = user_id)
ログインした際のオースuidとpostsテーブルのuser_idが合致したもの
このような形となる