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

今回の技術はnext13とsubabaseでログイン画面の認可・認証を行う際に、ユーザーによるデータ制御について行った
参考資料
詳しい!

サンプルのテーブル
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が合致したもの
このような形となる