The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat: add auth hooks starter templates (#19349)
Joel Lee2f21a7426ae6
diff --git a/apps/studio/components/interfaces/SQLEditor/SQLEditor.constants.ts b/apps/studio/components/interfaces/SQLEditor/SQLEditor.constants.tsindex 24e476929fb..b28a972a306 100644--- a/apps/studio/components/interfaces/SQLEditor/SQLEditor.constants.ts+++ b/apps/studio/components/interfaces/SQLEditor/SQLEditor.constants.ts@@ -1295,6 +1295,212 @@ create extension "supabase-dbdev";ORDER BY table_size DESCLIMIT 25`.trim(),},+ {+ id: 26,+ type: 'template',+ title: 'Limit MFA verification attempts to one in 2 seconds',+ description:+ 'Create an Auth hook that limits the number of failed MFA verification attempts to one in 2 seconds.',+ sql: `+create table public.mfa_failed_verification_attempts (+ user_id uuid not null,+ factor_id uuid not null,+ last_failed_at timestamp not null default now(),+ primary key (user_id, factor_id)+);++create function public.hook_mfa_verification_attempt(event jsonb)+ returns jsonb+ language plpgsql+ as $$+ declare+ last_failed_at timestamp;+ begin+ if event->'valid' is true+ then+ -- code is valid, accept it+ return jsonb_build_object('decision', 'continue');+ end if;++ select last_failed_at into last_failed_at+ from public.mfa_failed_verification_attempts+ where+ user_id = event->'user_id'+ and+ factor_id = event->'factor_id';++ if last_failed_at is not null and now() - last_failed_at < interval '2 seconds'+ then+ -- last attempt was done too quickly+ return jsonb_build_object(+ 'error', jsonb_build_object(+ 'http_code', 420,+ 'message', 'Please wait a moment before trying again.'));+ end if;++ -- record this failed attempt+ insert into public.mfa_failed_verification_attempts+ (+ user_id,+ factor_id,+ last_failed_at+ )+ values+ (+ event->'user_id',+ event->'factor_id',+ now()+ )+ on conflict+ do update+ set last_failed_at = now();++ -- finally let Supabase Auth do the default behavior for a failed attempt+ return jsonb_build_object('decision', 'continue');+ end;+ $$;+-- Configure appropriate permissions+grant execute on function public.hook_mfa_verification_attempt to supabase_auth_admin;+grant all on table public.mfa_failed_verification_attempts to supabase_auth_admin;++revoke execute on function public.hook_mfa_verification_attempt from authenticated, anon;+revoke all on table public.mfa_failed_verification_attempts from authenticated, anon;++ `.trim(),+ },+ {+ id: 27,+ type: 'template',+ title: 'Add Auth Hook (Password Verification Attempt)',+ description:+ 'Create an Auth Hook that limits number of failed password verification attempts to one in 10 seconds',+ sql: `+ create table public.password_failed_verification_attempts (+ user_id uuid not null,+ last_failed_at timestamp not null default now(),+ primary key (user_id, factor_id)+ );+create function public.hook_password_verification_attempt(event jsonb)+ returns jsonb+ language plpgsql+ as $$+ declare+ last_failed_at timestamp;+ begin+ if event->'valid' is true+ then+ -- code is valid, accept it+ return jsonb_build_object('decision', 'continue');+ end if;++ select last_failed_at into last_failed_at+ from public.password_failed_verification_attempts+ where+ user_id = event->'user_id'++ if last_failed_at is not null and now() - last_failed_at < interval '10 seconds'+ then+ -- last attempt was done too quickly+ return jsonb_build_object(+ 'error', jsonb_build_object(+ 'http_code', 420,+ 'message', 'Please wait a moment before trying again.'));+ end if;++ -- record this failed attempt+ insert into public.password_failed_verification_attempts+ (+ user_id,+ last_failed_at+ )+ values+ (+ event->'user_id',+ now()+ )+ on conflict+ do update+ set last_failed_at = now();++ -- finally let Supabase Auth do the default behavior for a failed attempt+ return jsonb_build_object('decision', 'continue');+ end;+ $$;+-- Configure appropriate permissions+grant execute on function public.hook_password_verification_attempt to supabase_auth_admin;+grant all on table public.password_failed_verification_attempts to supabase_auth_admin;++revoke execute on function public.hook_password_verification_attempt from authenticated, anon;+revoke all on table public.password_failed_verification_attempts from authenticated, anon;+ `.trim(),+ },+ {+ id: 28,+ type: 'template',+ title: 'Add Auth Hook (Custom Access Token)',+ description: 'Create an Auth Hook to add custom claims to your Auth Token',+ sql: `+ create or replace function public.custom_access_token_hook(event jsonb)+ returns jsonb+ language plpgsql+ as $$+ declare+ claims jsonb;+ user_email text;+begin+ -- Fetch the email based on user_id+ select email into user_email from users where id = event->>'user_id';++ -- Check if the email has suffix '@supabase.com'+ if user_email like '%@supabase.com' then+ claims := event->'claims';++ -- check if 'user_metadata' exists in claims+ if jsonb_typeof(claims->'user_metadata') is null then+ -- if 'user_metadata' does not exist, create an empty object+ claims := jsonb_set(claims, '{user_metadata}', '{}');+ end if;++ -- set a claim of 'admin'+ claims := jsonb_set(claims, '{user_metadata, admin}', 'true');++ -- update the 'claims' object in the original event+ event := jsonb_set(event, '{claims}', claims);+ end if;++ -- return the modified or original event+ return event;+end;+$$;+-- Permissions for the hook+grant execute on function public.custom_access_token_hook to supabase_auth_admin;+revoke execute on function public.custom_access_token_hook from authenticated, anon;+ `.trim(),+ },++ {+ id: 29,+ type: 'template',+ title: 'Add Auth Hook (General)',+ description: 'Create an Auth Hook',+ sql: `+ create or replace function public.custom_access_token_hook(event jsonb)+ returns jsonb+ language plpgsql+ as $$+ declare+ -- Insert variables here+ begin+ -- Insert logic here++ return event;+end;+ $$;+-- Permissions for the hook+grant execute on function public.custom_access_token_hook to supabase_auth_admin;+revoke execute on function public.custom_access_token_hook from authenticated, anon;+ `,+ },]export const SQL_SNIPPET_SCHEMA_VERSION = '1.0'