Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat: fix indentation on hook SQL template snippets (#19900)

Joel Leed9e061c36e3a
diff --git a/apps/studio/components/interfaces/SQLEditor/SQLEditor.constants.ts b/apps/studio/components/interfaces/SQLEditor/SQLEditor.constants.ts
index b28a972a306..082b9396d2d 100644
--- a/apps/studio/components/interfaces/SQLEditor/SQLEditor.constants.ts
+++ b/apps/studio/components/interfaces/SQLEditor/SQLEditor.constants.ts
@@ -1302,71 +1302,72 @@ create extension "supabase-dbdev";
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
+ 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;
+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;
- `.trim(),
+ 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', 429,
+ '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_refreshed_at
+ )
+ values
+ (
+ event->'user_id',
+ event->'factor_id',
+ now()
+ )
+ on conflict do update
+ set last_refreshed_at = now();
+
+ -- finally let Supabase Auth do the default behavior for a failed attempt
+ return jsonb_build_object('decision', 'continue');
+ end;
+$$;
+
+-- Assign appropriate permissions and revoke access
+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,
@@ -1375,64 +1376,68 @@ revoke all on table public.mfa_failed_verification_attempts from authenticated,
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(),
+returns jsonb
+language plpgsql
+as $$
+ declare
+ last_failed_at timestamp;
+ begin
+ if event->'valid' is true then
+ -- password 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', 429,
+ '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;
+$$;
+
+-- Assign 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,
@@ -1440,41 +1445,47 @@ revoke all on table public.password_failed_verification_attempts from authentica
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
+-- Assumes that there is an is_admin flag on the profiles table.
+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';
+ is_admin boolean;
+ begin
+ -- Check if the user is marked as admin in the profiles table
+ select is_admin into is_admin from profiles where user_id = event->>'user_id';
- -- Check if the email has suffix '@supabase.com'
- if user_email like '%@supabase.com' then
- claims := event->'claims';
+ -- Proceed only if the user is an admin
+ if is_admin 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;
+ -- 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');
+ -- 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);
+ -- Update the 'claims' object in the original event
+ event := jsonb_set(event, '{claims}', claims);
end if;
- -- return the modified or original event
+ -- Return the modified or original event
return event;
-end;
+ 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;
+
+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(),
},
@@ -1484,18 +1495,17 @@ revoke execute on function public.custom_access_token_hook from authenticated, a
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;
+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;