The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
docs: add http auth hook docs (#25880)
Joel Leea8f6b74dd362
diff --git a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.tsindex 11d3b8eb12c..32f871da9f8 100644--- a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts+++ b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts@@ -634,7 +634,32 @@ export const auth = {name: 'Redirect URLs',url: '/guides/auth/redirect-urls',},- { name: 'Auth Hooks', url: '/guides/auth/auth-hooks' },+ {+ name: 'Auth Hooks',+ url: '/guides/auth/auth-hooks',+ items: [+ {+ name: 'Custom access token hook',+ url: '/guides/auth/auth-hooks/custom-access-token-hook',+ },+ {+ name: 'Send SMS hook',+ url: '/guides/auth/auth-hooks/send-sms-hook',+ },+ {+ name: 'Send email hook',+ url: '/guides/auth/auth-hooks/send-email-hook',+ },+ {+ name: 'MFA verification hook',+ url: '/guides/auth/auth-hooks/mfa-verification-hook',+ },+ {+ name: 'Password verification hook',+ url: '/guides/auth/auth-hooks/password-verification-hook',+ },+ ],+ },{ name: 'Custom SMTP', url: '/guides/auth/auth-smtp' },{ name: 'User Management', url: '/guides/auth/managing-user-data' },],diff --git a/apps/docs/content/guides/auth/auth-hooks.mdx b/apps/docs/content/guides/auth/auth-hooks.mdxindex 5986aeb6d0c..9ea4d82c4b7 100644--- a/apps/docs/content/guides/auth/auth-hooks.mdx+++ b/apps/docs/content/guides/auth/auth-hooks.mdx@@ -1,10 +1,12 @@---id: 'auth-hooks'title: 'Auth Hooks'-description: 'Use Supabase Postgres Functions to customize your authentication flow'+subtitle: 'Use HTTP or Postgres Functions to customize your authentication flow'----Supabase allows you to use Postgres functions to alter the default Supabase Auth flow. Developers can use hooks to add custom behavior that's not supported natively.+## What is a hook++A hook is an endpoint that allows you to alter the default Supabase Auth flow at specific execution points. Developers can use hooks to add custom behavior that's not supported natively.Hooks help you:@@ -12,56 +14,52 @@ Hooks help you:- Improve security by adding additional checks to password and multi-factor authentication- Support legacy systems by integrating with identity credentials from external authentication systems- Add additional custom claims to your JWT+- Send authentication emails or SMS messages through a custom provider-You can use hooks at specific points along an Auth flow to perform custom behavior via Postgres Functions. The following hooks are available:--| Hook | Available on Plan |-| ----------------------------- | ------------------- |-| Custom Access Token | Free, Pro |-| MFA Verification Attempt | Team and Enterprise |-| Password Verification Attempt | Team and Enterprise |+The following hooks are available:-You can connect a hook to Supabase Auth to signal to Supabase Auth that it should make use of the hook.+| Hook | Available on Plan |+| -------------------------------------------------------------------------------------- | -------------------- |+| [Custom Access Token](/docs/guides/auth/hooks/custom-access-token) | Free, Pro |+| [Send SMS](/docs/guides/auth/hooks/send-sms) | Free, Pro |+| [Send Email](/docs/guides/auth/hooks/send-email) | Free, Pro |+| [MFA Verification Attempt](/docs/guides/auth/hooks/mfa-verification-attempt) | Teams and Enterprise |+| [Password Verification Attempt](/docs/guides/auth/hooks/password-verification-attempt) | Teams and Enterprise |-## Create a hook+Supabase supports 2 ways to [configure a hook](/dashboard/project/_/auth/hooks) in your project:-### What is a hook+<Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="postgres-function"+>+<TabPanel id="postgres-function" label="Postgres Function">-A hook is a [Postgres Function](https://www.postgresql.org/docs/current/sql-createfunction.html) with a single argument -- the `event` of type [JSONB]() -- and which returns a JSONB object.+A [postgres function](/docs/guides/database/functions) can be configured as a hook. The function should take in a single argument -- the event of type JSONB -- and return a JSONB object. Since the postgres function runs on your database, the request does not leave your project's instance.-This function is invoked by Supabase Auth at its point in the flow by providing an event object. The object returned by the function gives instructions on how Supabase Auth should continue processing.+</TabPanel>+<TabPanel id="http" label="HTTP Endpoint">-To access properties of the `event` argument, you can use the [JSON operators and functions](https://www.postgresql.org/docs/current/functions-json.html).+A HTTP Hook is an endpoint which takes in a JSON event payload and returns a JSON response. You can use any HTTP endpoint as a Hook, including an endpoint in your application. The easiest way to create a HTTP hook is to create a [Supabase Edge Function](/docs/guides/functions/quickstart).-There are no restrictions as to what language can be used to write Auth Hooks. If [PL/pgSQL](https://www.postgresql.org/docs/current/plpgsql.html) is too difficult consider using the [plv8](/docs/guides/database/extensions/plv8) extension which lets you use JavaScript to define functions.+</TabPanel>+</Tabs>-<Admonition type="caution">- If you're using the Supabase SQL Editor, there's an issue when using the `?` (*Does the string- exist as a top-level key within the JSON value?*) operator. Use a direct connection to the- database if you need to use it when defining a function.-</Admonition>+## Security Model-Here is an example hook signature:+Sign the payload and grant permissions selectively in order to guard the integrity of the payload.-```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;-$$;-```--You can visit `SQL Editor > Templates` for hook templates.--### Hook grants and roles+<Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="sql"+ queryGroup="language"+>+<TabPanel id="sql" label="SQL">-You need to assign additional permissions so that Supabase Auth can access the hook as well as the tables it interacts with.+You will need to assign additional permissions so that Supabase Auth can access the hook as well as the tables it interacts with.The `supabase_auth_admin` role does not have permissions to the `public` schema. You need to grant the role permission to execute your hook function:@@ -91,645 +89,297 @@ We recommend that you do not use any tag and explicitly grant permissions to `suRead more about the `security definer` tag [in our database guide](/docs/guides/database/functions#security-definer-vs-invoker).-### Hook errors+</TabPanel>+<TabPanel id="http" label="HTTP">-You should return an error when facing a runtime error. Runtime errors are specific to your application and arise from specific business rules rather than programmer errors.+HTTP Hooks in Supabase follow the [Standard Webhooks Specification](https://www.standardwebhooks.com/), which is a set of guidelines aligning how hooks are implemented. The specification attaches three security headers to guarantee the integrity of the payload:-Runtime errors could happen when:+- `webhook-id`: the unique webhook identifier described in the preceding sections.+- `webhook-timestamp`: integer UNIX timestamp (seconds since epoch).+- `webhook-signature`: the signatures of this webhook. This is generated from body of the hook.-- The user does not have appropriate permissions-- The event payload received does not have required claims.-- The user has performed an action which violates a business rule.+When the request is made to the HTTP hook, you should use the [Standard Webhooks libraries](https://github.com/standard-webhooks/standard-webhooks/tree/main/libraries) to verify these headers.-The error is a JSON object and has the following properties:+When a HTTP hook is created, the secret generated should be of the following format: `v1,whsec_<standard-base64-secret>`:-- `error` An object that contains information about the error.- - `http_code` A number indicating the HTTP code to be returned. If not set, the code is HTTP 500 Internal Server Error.- - `message` A message to be returned in the HTTP response. Required.+- `v1` denotes the version of the hook+- `whsec_` signifies that the secret is symmetric+- `<standard-base64-secret>` implies a Standard Base64 encoded secret which can contain the characters `+`, `/` and `=`-Here's an example:+The secret is used to verify the payload received in your hook. Create an entry in your `.env.local` file to store the secret for each hook that you have. For example:-```json-{- "error": {- "http_code": 429,- "message": "You can only verify a factor once every 10 seconds."- }-}+```bash+HOOK_SECRET=<standard-base64-secret>```-When an error is returned, the error is propagated from the hook to Supabase Auth and translated into a HTTP error which is returned to your application. Supabase Auth will only take into account the error and disregard the rest of the payload.--### Timeouts--Ensure that your hooks complete within **2 seconds** to avoid any errors.--### Connect a hook--In the dashboard, navigate to [`Authentication > Hooks (Beta)`](/dashboard/project/_/auth/hooks) and select the appropriate PostgreSQL function from the dropdown menu.--## Hook: MFA verification attempt--You can add additional checks to the [Supabase MFA implementation](/docs/guides/auth/auth-mfa) with hooks. For example, you can:--- Limit the number of verification attempts performed over a period of time.-- Sign out users who have too many invalid verification attempts.-- Count, rate limit, or ban sign-ins.+Use the secret in conjunction with the Standard Webhooks package to verify the payload before processing it:-**Inputs**+```jsx+import { Webhook } from 'https://esm.sh/[email protected]'-Supabase Auth will supply the following fields to your hook:+Deno.serve(async (req) => {+ const payload = await req.text()+ const hookSecret = Deno.env.get('HOOK_SECRET')+ // Extract headers and security specific fields+ const headers = Object.fromEntries(req.headers)+ const wh = new Webhook(hookSecret)+ const data = wh.verify(payload, headers)-- `factor_id` Unique identifier for the MFA factor being verified.-- `user_id` Unique identifier for the user.-- `valid` Whether the verification attempt was valid. For TOTP, this means that the six digit code was correct (true) or incorrect (false).--Example payload:--```json-{- "factor_id": "6eab6a69-7766-48bf-95d8-bd8f606894db",- "user_id": "3919cb6e-4215-4478-a960-6d3454326cec",- "valid": true-}+ // Payload data is verified, continue with business logic here+ // ...+})```-**Outputs**+</TabPanel>+</Tabs>-Return this if your hook processed the input without errors.+## Using Hooks-- `decision` A string containing the decision on whether to allow authentication to move forward. Use `reject` to deny the verification attempt and log the user out of all active sessions. Use `continue` to use the default Supabase Auth behavior.-- `message` The message to show the user if the decision was `reject`.+### Developing-Example output:+Let us develop a Hook locally and then deploy it to the cloud. As a recap, here’s a list of available Hooks-```json-{- "decision": "reject",- "message": "You have exceeded maximum number of MFA attempts."-}-```+| Hook | Suggested Function Name | When it is called | What it Does |+| ----------------------------- | ------------------------------- | -------------------------------------------------- | --------------------------------------------------------------------------------------------------------- |+| Send SMS | `send_sms` | Each time an SMS is sent | Allows you to customize message content and SMS Provider |+| Send Email | `send_email` | Each time an Email is sent | Allows you to customize message content and Email Provider |+| Custom Access Token | `custom_access_token` | Each time a new JWT is created | Returns the claims you wish to be present in the JWT. |+| MFA Verification Attempt | `mfa_verification_attempt` | Each time a user tries to verify an MFA factor. | Returns a decision on whether to reject the attempt and future ones, or to allow the user to keep trying. |+| Password Verification Attempt | `password_verification_attempt` | Each time a user tries to sign in with a password. | Return a decision whether to allow the user to reject the attempt, or to allow the user to keep trying. |-**Examples**+Edit `config.toml` to set up the Auth Hook locally.<Tabsscrollablesize="small"type="underlined"- defaultActiveId="limit-failed-mfa-verification-attempts"+ defaultActiveId="sql"queryGroup="language">-<TabPanel id="limit-failed-mfa-verification-attempts" label="Limit failed MFA verification attempts">--Your company requires that a user can input an incorrect MFA Verification code no more than once every 2 seconds.+<TabPanel id="sql" label="SQL">+Modify the `auth.hook.<hook_name>` field and set `uri` to a value of `pg-functions://postgres/<schema>/<function_name>`-Create a table to record the last time a user had an incorrect MFA verification attempt for a factor.+```+[auth.hook.<hook_name>]+enabled = true+uri = "pg-functions://...."-```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 a hook to read and write information to this table. For example:+You need to assign additional permissions so that Supabase Auth can access the hook as well as the tables it interacts with.-```sql-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', 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;-$$;+The `supabase_auth_admin` role does not have permissions to the `public` schema. You need to grant the role permission to execute your hook:--- Assign appropriate permissions and revoke access+```sqlgrant execute- on function public.hook_mfa_verification_attempt- to supabase_auth_admin;--grant all- on table public.mfa_failed_verification_attempts+ on function public.custom_access_token_hookto supabase_auth_admin;-revoke execute- on function public.hook_mfa_verification_attempt- from authenticated, anon, public;--revoke all- on table public.mfa_failed_verification_attempts- from authenticated, anon, public;```-</TabPanel>-</Tabs>--## Hook: Password verification attempt--Your company wishes to increase security beyond the requirements of the default password implementation in order to fulfill security or compliance requirements. You plan to track the status of a password sign-in attempt and take action via an email or a restriction on logins where necessary.+You also need to grant usage to `supabase_auth_admin`:-As this hook runs on unauthenticated requests, malicious users can abuse the hook by calling it multiple times. Pay extra care when using the hook as you can unintentionally block legitimate users from accessing your application.+```sql+grant usage on schema public to supabase_auth_admin;-Check if a password is valid prior to taking any additional action to ensure the user is legitimate. Where possible, send an email or notification instead of blocking the user.+```-**Inputs**+Also revoke permissions from the `authenticated` and `anon` roles to ensure the function is not accessible by Supabase Serverless APIs.-- `user_id` Unique identifier for the user attempting to sign in. Correlate this to the `auth.users` table.-- `valid` Whether the password verification attempt was valid.+```sql+revoke execute+ on function public.custom_access_token_hook+ from authenticated, anon;-```json-{- "user_id": "3919cb6e-4215-4478-a960-6d3454326cec",- "valid": true-}```-**Outputs**--Return these only if your hook processed the input without errors.--- `decision` A string containing the decision whether to allow authentication to move forward. Use `reject` to completely reject the verification attempt and log the user out of all active sessions. Use `continue` to use the default Supabase Auth behavior.-- `message` The message to show the user if the decision was `reject`.-- `should_logout_user` Whether to logout a user if a `reject` decision is issued. Has no effect when a `continue` decision is issued.+For security, we recommend against the use the `security definer` tag. The `security definer` tag specifies that the function is to be executed with the privileges of the user that owns it. When a function is created via the Supabase dashboard with the tag, it will have the extensive permissions of the `postgres` role which make it easier for undesirable actions to occur.-Example output:+We recommend that you do not use any tag and explicitly grant permissions to `supabase_auth_admin` as described above.-```json-{- "decision": "reject",- "message": "You have exceeded maximum number of password sign-in attempts.",- "should_logout_user": "false"-}-```+Read more about `security definer` tag [in our database guide](/docs/guides/database/functions#security-definer-vs-invoker).-**Examples**+There are no restrictions as to what language can be used to write a Postgres Hook. If [PL/pgSQL](https://www.postgresql.org/docs/current/plpgsql.html) is too difficult consider using the [plv8](/docs/guides/database/extensions/plv8) extension which lets you use JavaScript to define functions.-<Tabs- scrollable- size="small"- type="underlined"- defaultActiveId="limit-failed-password-verification-attempts"- queryGroup="language"->-<TabPanel id="limit-failed-password-verification-attempts" label="Limit failed password verification attempts">+Once done, save your Auth Hook as a migration in order to version the Auth Hook and share it with other team members. Run [`supabase migration new`](/docs/reference/cli/supabase-migration-new) to create a migration.-As part of new security measures within the company, users can only input an incorrect password every 10 seconds and not more than that. You want to write a hook to enforce this.+<Admonition type="caution">-Create a table to record each user's last incorrect password verification attempt.+If you're using the Supabase SQL Editor, there's an issue when using the `?` (_Does the string exist as a top-level key within the JSON value?_) operator. Use a direct connection to the database if you need to use it when defining a function.-```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)-);-```+</Admonition>-Create a hook to read and write information to this table. For example:+Here is an example hook signature:```sql-create function public.hook_password_verification_attempt(event jsonb)+create or replace function public.custom_access_token_hook(event jsonb)returns jsonblanguage plpgsqlas $$- 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;+declare+ -- Insert variables here+begin+ -- Insert logic here+ return event;+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, public;--revoke all- on table public.password_failed_verification_attempts- from authenticated, anon, public;```+You can visit `SQL Editor > Templates` for hook templates.+</TabPanel>-<TabPanel id="send-email-on-failed-password-attempt" label="Send email notification on failed password attempts">+<TabPanel id="http" label="HTTP">+Modify the `auth.hook.<hook_name>` field and set `uri` to a valid HTTP URI. For example, the `send_sms` hook would take the following fields:+```+[auth.hook.send_sms]+enabled = true+uri = "http://host.docker.internal:54321/functions/v1/send_sms"+secret = "env(SEND_SMS_HOOK_SECRET)"+```-You can notify a user via email instead of blocking the user. To do so, make use of [Supabase Vault](/docs/guides/database/vault) to store the API Key of our mail provider and use [`pg_net`](/docs/guides/database/extensions/pg_net) to send a HTTP request to our email provider to send the email. Ensure that you have configured a sender signature for the email account which you are sending emails from.+`host.docker.internal` is a special DNS name used in Docker to allow a container to access the host machine's network. This allows the Auth container to reach the Functions container-First, create a table to track sign in attempts.+Fill in the Hook Secret in `supabase/functions/.env`-```sql-create table public.password_sign_in_attempts (- user_id uuid not null,- attempt_id uuid not null,- last_attempt_at timestamp not null default now(),- attempt_successful boolean not null,- primary key (user_id, attempt_id)-);+```bash+SEND_SMS_HOOK_SECRET='v1,whsec_<base64_standard_secret>'```-Next, store the API key of our email API provider:+Start the function locally:-```sql-select vault.create_secret('my_api_key', 'my_api_key_name', 'description_of_my_api_key');+```bash+supabase functions serve send-sms --no-verify-jwt```-Create the hook:+Disable JWT verification via the `--no-verify-jwt` to accommodate hooks which are run before a JWT is issued. Payload authenticity is instead protected via the appended security headers associated with the Standard Webhooks Standard.-```sql-create or replace function public.hook_notify_user_on_failed_attempts(event jsonb)-returns jsonb-language plpgsql-as $$- declare- user_id uuid;- server_token text;- user_email_address text;- email_body jsonb;- response_id int; -- Variable to store the response ID- http_code int;- error_message jsonb;- attempt_count int;- max_attempts int := 5; -- Set the threshold for failed attempts- begin- user_id := (event->>'user_id')::uuid;-- -- Record the attempt- insert into public.password_sign_in_attempts (user_id, attempt_id, last_attempt_at, attempt_successful)- values (user_id, (event->>'attempt_id')::uuid, now(), (event->>'valid')::boolean)- on conflict (user_id, attempt_id)- do update set last_attempt_at = now(), attempt_successful = (event->>'valid')::boolean;-- -- Check failed attempts and fetch user email- select count(*), u.email into attempt_count, user_email_address- from public.password_sign_in_attempts a- join auth.users u on a.user_id = u.id- where a.user_id = user_id and attempt_successful = false and last_attempt_at > (now() - interval '1 day');-- -- Notify user if the number of failed attempts exceeds the threshold- if attempt_count >= max_attempts then- -- Fetch the server token- select decrypted_secret into server_token from vault.decrypted_secrets where name = 'my_api_key_name';-- -- Prepare the email body- email_body := format('{- "from": "[email protected]",- "to": "%s",- "subject": "Security Alert: Repeated Login Attempts Detected",- "textbody": "We have detected repeated login attempts for your account. If this was not you, please secure your account.",- "htmlbody": "<html><body><strong>Security Alert:</strong> We have detected repeated login attempts for your account. If this was not you, please secure your account.</body></html>",- "messagestream": "outbound"- }', user_email_address)::jsonb;-- -- Perform the HTTP POST request using Postmark- select id into response_id from net.http_post(- 'https://api.youremailprovider.com/email',- email_body,- 'application/json',- array['Accept: application/json', 'X-Postmark-Server-Token: ' || server_token]- );-- -- Fetch the response from net._http_response using the obtained id- select status_code, content into http_code, error_message from net._http_response where id = response_id;-- -- Handle email sending errors- if http_code is null or (http_code < 200 or http_code >= 300) then- return jsonb_build_object(- 'error', jsonb_build_object(- 'http_code', coalesce(http_code, 0),- 'message', coalesce(error_message ->> 'message', 'error sending email')- )- );- end if;- end if;-- -- Continue with default behavior- return jsonb_build_object('decision', 'continue');- end;-$$;---- Assign appropriate permissions-grant execute- on function public.hook_notify_user_on_failed_attempts- to supabase_auth_admin;--revoke execute- on function public.hook_notify_user_on_failed_attempts- from authenticated, anon, public;--grant all- on table public.password_sign_in_attempts- to supabase_auth_admin;--revoke all- on table public.password_sign_in_attempts- from authenticated, anon, public;-```+Note that payloads are sent uncompressed in order to accurately track Content Length. In addition, there is a 20KB payload limit to guard against payload stuffing attacks.</TabPanel></Tabs>-## Hook: Custom access token--The custom access token hook runs before a token is issued and allows you to add additional claims based on the authentication method used.--Claims returned must conform to our specification. Supabase Auth will check for these claims after the hook is run and return an error if they are not present.+### Deploying-These are the fields currently available on an access token:+In the dashboard, navigate to [`Authentication > Hooks`](/dashboard/project/_/auth/hooks) and select the appropriate function type (SQL or HTTP) from the dropdown menu.-Required Claims: `aud`, `exp`, `iat`, `sub`, `email`, `phone`, `role`, `aal`, `session_id`-Optional Claims: `jti`, `iss`, `nbf`, `app_metadata`, `user_metadata`, `amr`+### Error Handling-**Inputs**--- `user_id` Unique identifier for the user attempting to sign in. Correlate this to the `auth.users` table.-- `claims` Claims which are attached to the access token.-- `authentication_method` the authentication method used to request the access token. Possible values include:- `oauth`, `password`, `otp`, `totp`, `recovery`, `invite`, `sso/saml`, `magiclink`, `email/signup`, `email_change`, `token_refresh`.--**Outputs**--Return these only if your hook processed the input without errors.+You should return an error when facing a runtime error. Runtime errors are specific to your application and arise from specific business rules rather than programmer errors.-- `claims` A json containing the updated claims after the hook has been run.+Runtime errors could happen when:-**Examples**+- The user does not have appropriate permissions+- The event payload received does not have required claims.+- The user has performed an action which violates a business rule.+- The email or phone provider used in the webhook returned an error.<Tabsscrollablesize="small"type="underlined"- defaultActiveId="add-admin-role"+ defaultActiveId="sql"queryGroup="language">-<TabPanel id="add-admin-role" label="Add admin role">--You can allow registered admin users to perform restricted actions by granting an `admin` claim to their token.--Create a profiles table with an `is_admin` flag:--```sql-create table profiles (- user_id uuid not null primary key references auth.users (id),- is_admin boolean not null default false-);-```--Create a hook:--```sql-create or replace function public.custom_access_token_hook(event jsonb)-returns jsonb-language plpgsql-as $$- declare- claims jsonb;- 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')::uuid;-- -- Proceed only if the user is an admin- if is_admin then- claims := event->'claims';-- -- Check if 'app_metadata' exists in claims- if jsonb_typeof(claims->'app_metadata') is null then- -- If 'app_metadata' does not exist, create an empty object- claims := jsonb_set(claims, '{app_metadata}', '{}');- end if;-- -- Set a claim of 'admin'- claims := jsonb_set(claims, '{app_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;-$$;--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, public;--grant all- on table public.profiles- to supabase_auth_admin;--revoke all- on table public.profiles- from authenticated, anon, public;-```+<TabPanel id="sql" label="SQL">-</TabPanel>-<TabPanel id="add-metadata-claim-plv8" label="Add claim via plv8">-You can add custom claims to the access token. In this example, we fetch the level of an existing user from the `profiles` table and assign new users a default level of zero.--For convenience, we enable the `plv8` extension under [`Database > Extensions`](/dashboard/project/_/database/extensions) to run JavaScript within our query.--Create a `profiles` table to hold information:+The error is a JSON object and has the following properties:-```sql-create table profiles (- user_id uuid not null primary key references auth.users (id),- level integer,- is_admin boolean not null default false-);-```+- `error` An object that contains information about the error.+ - `http_code` A number indicating the HTTP code to be returned. If not set, the code is HTTP 500 Internal Server Error.+ - `message` A message to be returned in the HTTP response. Required.-```javascript-create or replace function custom_access_token_hook(event jsonb)-returns jsonb-language plv8-as $$- var user_level;-- // Fetch the current user's level from the profiles table- var result = plv8.execute("select level from public.profiles where user_id = $1", [event.user_id]);- if (result.length > 0) {- user_level = result[0].level;- } else {- // Assign a default level of 0 if no level is found- user_level = 0;- }+Here's an example:- // Check if 'claims' exists in the event object; if not, initialize it- if (!event.claims) {- event.claims = {};+```json+{+ "error": {+ "http_code": 429,+ "message": "You can only verify a factor once every 10 seconds."}-- // Update the level in the claims- event.claims.level = user_level;-- return event;-$$;--grant all- on table public.profiles- to supabase_auth_admin;--revoke all- on table public.profiles- from authenticated, anon, public;+}```-</TabPanel>-<TabPanel id="restrict-access-to-sso-users" label="Restrict access to SSO users">-You can restrict access to internal applications with a hook. For example, you can require that employees log in via [SAML Single Sign On (SSO)](/docs/guides/auth/sso/auth-sso-saml). You can exempt select employees from the policy via an allowlist.--```sql-create or replace function public.restrict_application_access(event jsonb)- returns jsonb- language plpgsql-as $function$-declare- authentication_method text;- email_claim text;- allowed_emails text[] := array['[email protected]', '[email protected]'];-begin- -- Extract email claim and authentication method- email_claim = event->'claims'->>'email';- authentication_method = event->'authentication_method';- -- Authentication methods come double quoted (e.g. "otp")- authentication_method = replace(authentication_method, '"', '');-- if email_claim ilike '%@supabase.io' or authentication_method = 'sso/saml' or email_claim = any(allowed_emails) then- return event;- end if;-- -- If none of the conditions are met, return an error- return jsonb_build_object(- 'error', jsonb_build_object(- 'http_code', 403,- 'message', 'Staging access is only allowed to team members. Please use your @company.com account instead'- )- );-end;-$function$-;--- manually added-grant execute- on function public.restrict_application_access- to supabase_auth_admin;--revoke execute- on function public.restrict_application_access- from authenticated, anon, public;-```+Errors returned from a Postgres Hook are not retry-able. When an error is returned, the error is propagated from the hook to Supabase Auth and translated into a HTTP error which is returned to your application. Supabase Auth will only take into account the error and disregard the rest of the payload.</TabPanel>-</Tabs>--## Local Development--Unlike the hosted platform, the local development dashboard does not have a user interface for linking a Hook. Instead, edit `config.toml` to link an Auth Hook and run it with your local setup. Modify the `auth.hook.<hook_name>` field and set `uri` to a value of `pg-functions://postgres/<schema>/<function_name>`--For instance, the `config.toml` for a Custom Access Token Hook which uses the function `public.custom_access_token_hook`:+<TabPanel id="http" label="HTTP">+Hooks return status codes based on the nature of the response. These status codes help determine the next steps in the processing flow:++| HTTP Status Code | Description | Example Usage |+| ---------------- | ------------------------------------------------------------- | ---------------------------------------------- |+| 200, 202, 204 | Valid response, proceed | Successful processing of the request |+| 429, 503 | Retry-able errors with Retry-after header supported | Temporary server overload or maintenance |+| 403, 400 | Treated as Internal Server Errors and return a 500 Error Code | Malformed requests or insufficient permissions |++Errors are responses which contain status codes 400 and above. On a retry-able error, such as an error with a `429` or `503` status code, HTTP Hooks will attempt up to three retries with a back-off of two seconds.++Here's a sample HTTP retry schedule:++| Time Since Start (HH:MM:SS) | Event | Notes |+| --------------------------- | ----------------------- | ------------------------------------------------- |+| 00:00:00 | Initial Attempt | Initial invocation begins. |+| 00:00:05 | Initial Attempt Timeout | Initial invocation must complete. |+| 00:00:07 | Retry Start #1 | After 2 sec delay, first retry begins. |+| 00:00:12 | Retry Timeout #1 | First retry timeout. |+| 00:00:14 | Retry Start #2 | After 2 sec delay, second retry begins. |+| 00:00:19 | Retry Timeout #2 | Second retry timeout. Returns an error on failure |++Return a retry-able error by attaching a appropriate status code (`429`, `503` ) and a non-empty `retry-after` header++```jsx+return new Response(+ JSON.stringify({+ error: `Failed to process the request: ${error}`,+ }),+ { status: 429, headers: { 'Content-Type': 'application/json', 'retry-after': 'true' } }+)```-...--[auth.hook.custom_access_token]-enabled = true-uri = "pg-functions://postgres/public/custom_access_token_hook"-...-```+Note that all responses, including error responses, need a `Content-Type` of `application/json` - not specifying the appropriate `Content-Type` will result in the function returning an error response. Supabase Auth will in turn return an Internal Server Error.-Save your Auth Hook as a migration in order to version the Auth Hook and share it with other team members. Run [`supabase migration new`](https://supabase.com/docs/reference/cli/supabase-migration-new) to create a migration.--### Debugging Your Hook+</TabPanel>+</Tabs>-Test the Postgres Function in isolation before enabling the hook by connecting to your database with `psql` and running `select <your_function_name>`. Where necessary, use [`RAISE` statements](https://www.postgresql.org/docs/current/plpgsql-errors-and-messages.html) to print information to the terminal.+Outside of runtime errors, both HTTP Hooks and Postgres Hooks return timeout errors. Postgres Hooks have <SharedData data="config">auth.hook_timeouts.postgres_hooks</SharedData> seconds to complete processing while HTTP Hooks should complete in <SharedData data="config">auth.hook_timeouts.http_hooks</SharedData> seconds. Both HTTP Hooks and Postgres Hooks are run in a transaction do limit the duration of execution to avoid delays in authentication process.++## Available Hooks++Each Hook description contains an example JSONSchema which you can use in conjunction with [JSONSchema Faker](https://json-schema-faker.js.org/) in order to generate a mock payload. For HTTP Hooks, you can also use [the Standard Webhooks Testing Tool](https://www.standardwebhooks.com/simulate) to simulate a request.++<div className="grid md:grid-cols-12 gap-4 not-prose">+ {[+ {+ name: 'Custom Access Token',+ description: 'Customize the access token issued by Supabase Auth',+ href: '/guides/auth/auth-hooks/custom-access-token-hook',+ },+ {+ name: 'Send SMS',+ description: 'Use a custom SMS provider to send authentication messages',+ href: '/guides/auth/auth-hooks/send-sms-hook',+ },+ {+ name: 'Send Email',+ description: 'Use a custom email provider to send authentication messages',+ href: '/guides/auth/auth-hooks/send-email-hook',+ },+ {+ name: 'MFA Verification',+ description: 'Add additional checks to the MFA verification flow',+ href: '/guides/auth/auth-hooks/mfa-verification-hook',+ },+ {+ name: 'Password verification',+ description: 'Add additional checks to the password verification flow',+ href: '/guides/auth/auth-hooks/password-verification-hook',+ },+ ].map((x) => (+ <div className="col-span-4" key={x.href}>+ <Link href={x.href} passHref>+ <GlassPanel title={x.name}>{x.description}</GlassPanel>+ </Link>+ </div>+ ))}+</div>diff --git a/apps/docs/content/guides/auth/auth-hooks/custom-access-token-hook.mdx b/apps/docs/content/guides/auth/auth-hooks/custom-access-token-hook.mdxnew file mode 100644index 00000000000..e563dec6cca--- /dev/null+++ b/apps/docs/content/guides/auth/auth-hooks/custom-access-token-hook.mdx@@ -0,0 +1,491 @@+---+id: 'custom-access-token-hook'+title: 'Custom Access Token Hook'+subtitle: 'Customize the access token issued by Supabase Auth'+---++The custom access token hook runs before a token is issued and allows you to add additional claims based on the authentication method used.++Claims returned must conform to our specification. Supabase Auth will check for these claims after the hook is run and return an error if they are not present.++These are the fields currently available on an access token:++Required Claims: `aud`, `exp`, `iat`, `sub`, `email`, `phone`, `role`, `aal`, `session_id`+Optional Claims: `jti`, `iss`, `nbf`, `app_metadata`, `user_metadata`, `amr`++**Inputs**++| Field | Type | Description |+| ----------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |+| `user_id` | `string` | Unique identifier for the user attempting to sign in. |+| `claims` | `object` | Claims which are included in the access token. |+| `authentication_method` | `string` | The authentication method used to request the access token. Possible values include: `oauth`, `password`, `otp`, `totp`, `recovery`, `invite`, `sso/saml`, `magiclink`, `email/signup`, `email_change`, `token_refresh`, `anonymous`. |++<Tabs+ scrollable+ size="small"+ type="underlined"+>++<TabPanel id="custom-access-token-json" label="JSON">+```json+{+ "user_id": "8ccaa7af-909f-44e7-84cb-67cdccb56be6",+ "claims": {+ "aud": "authenticated",+ "exp": 1715690221,+ "iat": 1715686621,+ "sub": "8ccaa7af-909f-44e7-84cb-67cdccb56be6",+ "email": "",+ "phone": "",+ "app_metadata": {},+ "user_metadata": {},+ "role": "authenticated",+ "aal": "aal1",+ "amr": [ { "method": "anonymous", "timestamp": 1715686621 } ],+ "session_id": "4b938a09-5372-4177-a314-cfa292099ea2",+ "is_anonymous": true+ },+ "authentication_method": "anonymous"+}+```+</TabPanel>+<TabPanel id="custom-access-token-json-schema" label="JSON Schema">++```json+{+ "type": "object",+ "properties": {+ "user_id": {+ "type": "string",+ "x-faker": "random.uuid"+ },+ "claims": {+ "type": "object",+ "properties": {+ "aud": {+ "type": "string",+ "x-faker": "random.word"+ },+ "exp": {+ "type": "integer",+ "x-faker": "date.future"+ },+ "iat": {+ "type": "integer",+ "x-faker": "date.past"+ },+ "sub": {+ "type": "string",+ "x-faker": "random.uuid"+ },+ "email": {+ "type": "string",+ "x-faker": "internet.email"+ },+ "phone": {+ "type": "string",+ "x-faker": {+ "fake": "{{phone.phoneNumber('+1##########')}}"+ }+ },+ "app_metadata": {+ "type": "object",+ "x-faker": "random.objectElement"+ },+ "user_metadata": {+ "type": "object",+ "x-faker": "random.objectElement"+ },+ "role": {+ "type": "string",+ "enum": ["anon", "authenticated"]+ },+ "aal": {+ "type": "string",+ "enum": ["aal1", "aal2", "aal3"]+ },+ "amr": {+ "type": "array",+ "items": {+ "type": "object",+ "properties": {+ "method": {+ "type": "string",+ "enum": [+ "oauth",+ "password",+ "otp",+ "totp",+ "recovery",+ "invite",+ "sso/saml",+ "magiclink",+ "email/signup",+ "email_change",+ "token_refresh",+ "anonymous"+ ]+ },+ "timestamp": {+ "type": "integer",+ "x-faker": "date.past"+ }+ },+ "required": ["method", "timestamp"]+ }+ },+ "session_id": {+ "type": "string",+ "x-faker": "random.uuid"+ },+ "is_anonymous": {+ "type": "boolean",+ "x-faker": "random.boolean"+ }+ },+ "required": [+ "aud",+ "exp",+ "iat",+ "sub",+ "email",+ "phone",+ "app_metadata",+ "user_metadata",+ "role",+ "aal",+ "amr",+ "session_id",+ "is_anonymous"+ ]+ },+ "authentication_method": {+ "type": "string",+ "enum": [+ "oauth",+ "password",+ "otp",+ "totp",+ "recovery",+ "invite",+ "sso/saml",+ "magiclink",+ "email/signup",+ "email_change",+ "token_refresh",+ "anonymous"+ ]+ }+ },+ "required": ["user_id", "claims", "authentication_method"]+}+```++</TabPanel>+</Tabs>++**Outputs**++Return these only if your hook processed the input without errors.++| Field | Type | Description |+| -------- | -------- | ----------------------------------------------- |+| `claims` | `object` | The updated claims after the hook has been run. |++<Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="sql"+ queryGroup="language"+>+<TabPanel id="sql" label="SQL">+<Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="add-admin-role"+>+<TabPanel id="add-admin-role" label="Add admin role">++You can allow registered admin users to perform restricted actions by granting an `admin` claim to their token.++Create a profiles table with an `is_admin` flag:++```sql+create table profiles (+ user_id uuid not null primary key references auth.users (id),+ is_admin boolean not null default false+);+```++Create a hook:++```sql+create or replace function public.custom_access_token_hook(event jsonb)+returns jsonb+language plpgsql+as $$+ declare+ claims jsonb;+ 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')::uuid;++ -- Proceed only if the user is an admin+ if is_admin then+ claims := event->'claims';++ -- Check if 'app_metadata' exists in claims+ if jsonb_typeof(claims->'app_metadata') is null then+ -- If 'app_metadata' does not exist, create an empty object+ claims := jsonb_set(claims, '{app_metadata}', '{}');+ end if;++ -- Set a claim of 'admin'+ claims := jsonb_set(claims, '{app_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;+$$;++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, public;++grant all+ on table public.profiles+ to supabase_auth_admin;++revoke all+ on table public.profiles+ from authenticated, anon, public;+```++</TabPanel>+<TabPanel id="add-metadata-claim-plv8" label="Add claim via plv8">+You can add custom claims to the access token. In this example, we fetch the level of an existing user from the `profiles` table and assign new users a default level of zero.++For convenience, we enable the `plv8` extension under [`Database > Extensions`](/dashboard/project/_/database/extensions) to run JavaScript within our query.++Create a `profiles` table to hold information:++```sql+create table profiles (+ user_id uuid not null primary key references auth.users (id),+ level integer,+ is_admin boolean not null default false+);+```++```sql+create or replace function custom_access_token_hook(event jsonb)+returns jsonb+language plv8+as $$+ var user_level;++ -- Fetch the current user's level from the profiles table+ var result = plv8.execute("select level from public.profiles where user_id = $1", [event.user_id]);+ if (result.length > 0) {+ user_level = result[0].level;+ } else {+ -- Assign a default level of 0 if no level is found+ user_level = 0;+ }++ -- Check if 'claims' exists in the event object; if not, initialize it+ if (!event.claims) {+ event.claims = {};+ }++ -- Update the level in the claims+ event.claims.level = user_level;++ return event;+$$;++grant all+ on table public.profiles+ to supabase_auth_admin;++revoke all+ on table public.profiles+ from authenticated, anon, public;+```++</TabPanel>+<TabPanel id="restrict-access-to-sso-users" label="Restrict access to SSO users">+You can restrict access to internal applications with a hook. For example, you can require that employees log in via [SAML Single Sign On (SSO)](/docs/guides/auth/sso/auth-sso-saml). You can exempt select employees from the policy via an allowlist.++```sql+create or replace function public.restrict_application_access(event jsonb)+ returns jsonb+ language plpgsql+as $function$+declare+ authentication_method text;+ email_claim text;+ allowed_emails text[] := array['[email protected]', '[email protected]'];+begin+ -- Extract email claim and authentication method+ email_claim = event->'claims'->>'email';+ authentication_method = event->'authentication_method';+ -- Authentication methods come double quoted (e.g. "otp")+ authentication_method = replace(authentication_method, '"', '');++ if email_claim ilike '%@supabase.io' or authentication_method = 'sso/saml' or email_claim = any(allowed_emails) then+ return event;+ end if;++ -- If none of the conditions are met, return an error+ return jsonb_build_object(+ 'error', jsonb_build_object(+ 'http_code', 403,+ 'message', 'Staging access is only allowed to team members. Please use your @company.com account instead'+ )+ );+end;+$function$+;+-- manually added+grant execute+ on function public.restrict_application_access+ to supabase_auth_admin;++revoke execute+ on function public.restrict_application_access+ from authenticated, anon, public;+```++</TabPanel>+</Tabs>+</TabPanel>+<TabPanel id="http" label="HTTP">+<Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="http-add-metadata-claim"+>+<TabPanel id="http-add-metadata-claim" label="Add claim">++Your company wishes to add assign permissions via the role claim on the `app_metadata` field. Add the role claim to the token via a Hook.++```javascript+import { Webhook } from 'https://esm.sh/[email protected]'+import { readAll } from 'https://deno.land/std/io/read_all.ts'+import * as base64 from 'https://denopkg.com/chiefbiiko/base64/mod.ts'++Deno.serve(async (req) => {+ const payload = await req.text()+ const base64_secret = Deno.env.get('AUTH_CUSTOM_ACCESS_TOKEN_SECRET')+ const headers = Object.fromEntries(req.headers)+ const wh = new Webhook(base64_secret)+ try {+ const { user_id, claims, authentication_method } = wh.verify(payload, headers)+ if (claims.app_metadata && claims.app_metadata.role) {+ claims['role'] = claims.app_metadata.role+ }+ return new Response(+ JSON.stringify({+ claims,+ }),+ {+ status: 200,+ headers: {+ 'Content-Type': 'application/json',+ },+ }+ )+ } catch (error) {+ return new Response(+ JSON.stringify({+ error: `Failed to process the request: ${error}`,+ }),+ {+ status: 500,+ headers: {+ 'Content-Type': 'application/json',+ },+ }+ )+ }+})+```++</TabPanel>+<TabPanel id="http-restrict-access-to-sso-users" label="Restrict access to SSO users">+You can restrict access to internal applications with a hook. For example, you can require that employees log in via [SAML Single Sign On (SSO)](/docs/guides/auth/sso/auth-sso-saml). You can exempt select employees from the policy via an allowlist.++```javascript+import { Webhook } from 'https://esm.sh/[email protected]'+import { readAll } from 'https://deno.land/std/io/read_all.ts'+import * as base64 from 'https://denopkg.com/chiefbiiko/base64/mod.ts'++Deno.serve(async (req) => {+ const payload = await req.text()+ const base64_secret = Deno.env.get('AUTH_CUSTOM_ACCESS_TOKEN_SECRET')+ const headers = Object.fromEntries(req.headers)+ const wh = new Webhook(base64_secret)+ try {+ const { user_id, claims, authentication_method } = wh.verify(payload, headers)++ // Check the condition+ const allowedEmails = ['[email protected]', '[email protected]']+ if (authentication_method === 'sso/saml' || allowedEmails.includes(claims.email)) {+ return new Response(+ JSON.stringify({+ claims,+ }),+ {+ status: 200,+ headers: {+ 'Content-Type': 'application/json',+ },+ }+ )+ } else {+ return new Response(+ JSON.stringify({+ error: 'Unauthorized',+ }),+ {+ status: 500,+ headers: {+ 'Content-Type': 'application/json',+ },+ }+ )+ }+ } catch (error) {+ return new Response(+ JSON.stringify({+ error: `Failed to process the request: ${error}`,+ }),+ {+ status: 500,+ headers: {+ 'Content-Type': 'application/json',+ },+ }+ )+ }+})+```++</TabPanel>+</Tabs>+</TabPanel>+</Tabs>diff --git a/apps/docs/content/guides/auth/auth-hooks/mfa-verification-hook.mdx b/apps/docs/content/guides/auth/auth-hooks/mfa-verification-hook.mdxnew file mode 100644index 00000000000..ca298e4670f--- /dev/null+++ b/apps/docs/content/guides/auth/auth-hooks/mfa-verification-hook.mdx@@ -0,0 +1,182 @@+---+id: 'mfa-verification-hook'+title: 'MFA Verification Hook'+---++You can add additional checks to the [Supabase MFA implementation](/docs/guides/auth/auth-mfa) with hooks. For example, you can:++- Limit the number of verification attempts performed over a period of time.+- Sign out users who have too many invalid verification attempts.+- Count, rate limit, or ban sign-ins.++**Inputs**++Supabase Auth will send a payload containing these fields to your hook:++| Field | Type | Description |+| ------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------- |+| `factor_id` | `string` | Unique identifier for the MFA factor being verified |+| `factor_type` | `string` | `totp` or `phone` |+| `user_id` | `string` | Unique identifier for the user |+| `valid` | `boolean` | Whether the verification attempt was valid. For TOTP, this means that the six digit code was correct (true) or incorrect (false). |++<Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="mfa-verification-attempt-json"+>+<TabPanel id="mfa-verification-attempt-json" label="JSON">++```json+{+ "factor_id": "6eab6a69-7766-48bf-95d8-bd8f606894db",+ "user_id": "3919cb6e-4215-4478-a960-6d3454326cec",+ "valid": true+}+```++</TabPanel>++<TabPanel id="mfa-verification-attempt-json-schema" label="JSON Schema">++```json+{+ "type": "object",+ "properties": {+ "user_id": {+ "type": "string",+ "x-faker": "random.uuid"+ },+ "valid": {+ "type": "boolean",+ "x-faker": "random.boolean"+ }+ },+ "required": ["user_id", "valid"]+}+```++</TabPanel>+</Tabs>++**Outputs**++Return this if your hook processed the input without errors.++| Field | Type | Description |+| ---------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |+| `decision` | `string` | The decision on whether to allow authentication to move forward. Use `reject` to deny the verification attempt and log the user out of all active sessions. Use `continue` to use the default Supabase Auth behavior. |+| `message` | `string` | The message to show the user if the decision was `reject`. |++```json+{+ "decision": "reject",+ "message": "You have exceeded maximum number of MFA attempts."+}+```++<Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="sql"+ queryGroup="language"+>+<TabPanel id="sql" label="SQL">+<Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="sql-limit-failed-mfa-verification-attempts"+>+<TabPanel id="sql-limit-failed-mfa-verification-attempts" label="Limit failed MFA verification attempts">++Your company requires that a user can input an incorrect MFA Verification code no more than once every 2 seconds.++Create a table to record the last time a user had an incorrect MFA verification attempt for a factor.++```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 a hook to read and write information to this table. For example:++```sql+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', 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, public;++revoke all+ on table public.mfa_failed_verification_attempts+ from authenticated, anon, public;+```++</TabPanel>+</Tabs>+</TabPanel>+</Tabs>diff --git a/apps/docs/content/guides/auth/auth-hooks/password-verification-hook.mdx b/apps/docs/content/guides/auth/auth-hooks/password-verification-hook.mdxnew file mode 100644index 00000000000..26d007d5c58--- /dev/null+++ b/apps/docs/content/guides/auth/auth-hooks/password-verification-hook.mdx@@ -0,0 +1,287 @@+---+id: 'password-verification-hook'+title: 'Password Verification Hook'+---++Your company wishes to increase security beyond the requirements of the default password implementation in order to fulfill security or compliance requirements. You plan to track the status of a password sign-in attempt and take action via an email or a restriction on logins where necessary.++As this hook runs on unauthenticated requests, malicious users can abuse the hook by calling it multiple times. Pay extra care when using the hook as you can unintentionally block legitimate users from accessing your application.++Check if a password is valid prior to taking any additional action to ensure the user is legitimate. Where possible, send an email or notification instead of blocking the user.++**Inputs**++| Field | Type | Description |+| --------- | --------- | ----------------------------------------------------------------------------------------------- |+| `user_id` | `string` | Unique identifier for the user attempting to sign in. Correlate this to the `auth.users` table. |+| `valid` | `boolean` | Whether the password verification attempt was valid. |++<Tabs+ scrollable+ size="small"+ type="underlined"+>+<TabPanel id="password-verification-attempt-json" label="JSON">++```json+{+ "user_id": "3919cb6e-4215-4478-a960-6d3454326cec",+ "valid": true+}+```++</TabPanel>+<TabPanel id="password-verification-attempt-json-schema" label="JSON Schema">++```json+{+ "type": "object",+ "properties": {+ "user_id": {+ "type": "string",+ "x-faker": "random.uuid"+ },+ "valid": {+ "type": "boolean",+ "x-faker": "random.boolean"+ }+ },+ "required": ["user_id", "valid"]+}+```++</TabPanel>+</Tabs>++**Outputs**++Return these only if your hook processed the input without errors.++| Field | Type | Description |+| -------------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |+| `decision` | `string` | The decision on whether to allow authentication to move forward. Use `reject` to deny the verification attempt and log the user out of all active sessions. Use `continue` to use the default Supabase Auth behavior. |+| `message` | `string` | The message to show the user if the decision was `reject`. |+| `should_logout_user` | `boolean` | Whether to log out the user if a `reject` decision is issued. Has no effect when a `continue` decision is issued. |++```json+{+ "decision": "reject",+ "message": "You have exceeded maximum number of password sign-in attempts.",+ "should_logout_user": "false"+}+```++<Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="sql"+ queryGroup="language"+>+<TabPanel id="sql" label="SQL">+<Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="sql-limit-failed-password-verification-attempts"+>+<TabPanel id="sql-limit-failed-password-verification-attempts" label="Limit failed password verification attempts">++As part of new security measures within the company, users can only input an incorrect password every 10 seconds and not more than that. You want to write a hook to enforce this.++Create a table to record each user's last incorrect password verification attempt.++```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)+);+```++Create a hook to read and write information to this table. For example:++```sql+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+ -- 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, public;++revoke all+ on table public.password_failed_verification_attempts+ from authenticated, anon, public;+```++</TabPanel>+<TabPanel id="sql-send-email-on-failed-password-attempt" label="Send email notification on failed password attempts">++You can notify a user via email instead of blocking the user. To do so, make use of [Supabase Vault](/docs/guides/database/vault) to store the API Key of our mail provider and use [`pg_net`](/docs/guides/database/extensions/pg_net) to send a HTTP request to our email provider to send the email. Ensure that you have configured a sender signature for the email account which you are sending emails from.++First, create a table to track sign in attempts.++```sql+create table public.password_sign_in_attempts (+ user_id uuid not null,+ attempt_id uuid not null,+ last_attempt_at timestamp not null default now(),+ attempt_successful boolean not null,+ primary key (user_id, attempt_id)+);+```++Next, store the API key of our email API provider:++```sql+select vault.create_secret('my_api_key', 'my_api_key_name', 'description_of_my_api_key');+```++Create the hook:++```sql+create or replace function public.hook_notify_user_on_failed_attempts(event jsonb)+returns jsonb+language plpgsql+as $$+ declare+ user_id uuid;+ server_token text;+ user_email_address text;+ email_body jsonb;+ response_id int; -- Variable to store the response ID+ http_code int;+ error_message jsonb;+ attempt_count int;+ max_attempts int := 5; -- Set the threshold for failed attempts+ begin+ user_id := (event->>'user_id')::uuid;++ -- Record the attempt+ insert into public.password_sign_in_attempts (user_id, attempt_id, last_attempt_at, attempt_successful)+ values (user_id, (event->>'attempt_id')::uuid, now(), (event->>'valid')::boolean)+ on conflict (user_id, attempt_id)+ do update set last_attempt_at = now(), attempt_successful = (event->>'valid')::boolean;++ -- Check failed attempts and fetch user email+ select count(*), u.email into attempt_count, user_email_address+ from public.password_sign_in_attempts a+ join auth.users u on a.user_id = u.id+ where a.user_id = user_id and attempt_successful = false and last_attempt_at > (now() - interval '1 day');++ -- Notify user if the number of failed attempts exceeds the threshold+ if attempt_count >= max_attempts then+ -- Fetch the server token+ select decrypted_secret into server_token from vault.decrypted_secrets where name = 'my_api_key_name';++ -- Prepare the email body+ email_body := format('{+ "from": "[email protected]",+ "to": "%s",+ "subject": "Security Alert: Repeated Login Attempts Detected",+ "textbody": "We have detected repeated login attempts for your account. If this was not you, please secure your account.",+ "htmlbody": "<html><body><strong>Security Alert:</strong> We have detected repeated login attempts for your account. If this was not you, please secure your account.</body></html>",+ "messagestream": "outbound"+ }', user_email_address)::jsonb;++ -- Perform the HTTP POST request using Postmark+ select id into response_id from net.http_post(+ 'https://api.youremailprovider.com/email',+ email_body,+ 'application/json',+ array['Accept: application/json', 'X-Postmark-Server-Token: ' || server_token]+ );++ -- Fetch the response from net._http_response using the obtained id+ select status_code, content into http_code, error_message from net._http_response where id = response_id;++ -- Handle email sending errors+ if http_code is null or (http_code < 200 or http_code >= 300) then+ return jsonb_build_object(+ 'error', jsonb_build_object(+ 'http_code', coalesce(http_code, 0),+ 'message', coalesce(error_message ->> 'message', 'error sending email')+ )+ );+ end if;+ end if;++ -- Continue with default behavior+ return jsonb_build_object('decision', 'continue');+ end;+$$;++-- Assign appropriate permissions+grant execute+ on function public.hook_notify_user_on_failed_attempts+ to supabase_auth_admin;++revoke execute+ on function public.hook_notify_user_on_failed_attempts+ from authenticated, anon, public;++grant all+ on table public.password_sign_in_attempts+ to supabase_auth_admin;++revoke all+ on table public.password_sign_in_attempts+ from authenticated, anon, public;+```++</TabPanel>+</Tabs>+</TabPanel>+</Tabs>diff --git a/apps/docs/content/guides/auth/auth-hooks/send-email-hook.mdx b/apps/docs/content/guides/auth/auth-hooks/send-email-hook.mdxnew file mode 100644index 00000000000..b91fb1d9c9c--- /dev/null+++ b/apps/docs/content/guides/auth/auth-hooks/send-email-hook.mdx@@ -0,0 +1,756 @@+---+id: 'send-email-hook'+title: 'Send Email Hook'+subtitle: 'Use a custom email provider to send authentication messages'+---++The Send Email Hook runs before an email is sent and allows for flexibility around email sending. You can use this hook to configure a back-up email provider or add internationalization to your emails.++**Inputs**++| Field | Type | Description |+| ------- | ------------------------------------------------- | ---------------------------------------------------------------------------------- |+| `user` | [`User`](/docs/guides/auth/users#the-user-object) | The user attempting to sign in. |+| `email` | `object` | Metadata specific to the email sending process. Includes the OTP and `token_hash`. |++<Tabs+ scrollable+ size="small"+ type="underlined"+>+<TabPanel id="send-email-json" label="JSON">++```json+{+ "user": {+ "id": "8484b834-f29e-4af2-bf42-80644d154f76",+ "aud": "authenticated",+ "role": "authenticated",+ "email": "[email protected]",+ "phone": "",+ "app_metadata": {+ "provider": "email",+ "providers": ["email"]+ },+ "user_metadata": {+ "email": "[email protected]",+ "email_verified": false,+ "phone_verified": false,+ "sub": "8484b834-f29e-4af2-bf42-80644d154f76"+ },+ "identities": [+ {+ "identity_id": "bc26d70b-517d-4826-bce4-413a5ff257e7",+ "id": "8484b834-f29e-4af2-bf42-80644d154f76",+ "user_id": "8484b834-f29e-4af2-bf42-80644d154f76",+ "identity_data": {+ "email": "[email protected]",+ "email_verified": false,+ "phone_verified": false,+ "sub": "8484b834-f29e-4af2-bf42-80644d154f76"+ },+ "provider": "email",+ "last_sign_in_at": "2024-05-14T12:56:33.824231484Z",+ "created_at": "2024-05-14T12:56:33.824261Z",+ "updated_at": "2024-05-14T12:56:33.824261Z",+ "email": "[email protected]"+ }+ ],+ "created_at": "2024-05-14T12:56:33.821567Z",+ "updated_at": "2024-05-14T12:56:33.825595Z",+ "is_anonymous": false+ },+ "email_data": {+ "token": "305805",+ "token_hash": "7d5b7b1964cf5d388340a7f04f1dbb5eeb6c7b52ef8270e1737a58d0",+ "redirect_to": "http://localhost:3000/",+ "email_action_type": "signup",+ "site_url": "http://localhost:9999",+ "token_new": "",+ "token_hash_new": ""+ }+}+```++</TabPanel>+<TabPanel id="send-email-json-schema" label="JSON Schema">++```json+{+ "type": "object",+ "properties": {+ "user": {+ "type": "object",+ "properties": {+ "id": {+ "type": "string",+ "x-faker": "random.uuid"+ },+ "aud": {+ "type": "string",+ "enum": ["authenticated"]+ },+ "role": {+ "type": "string",+ "enum": ["anon", "authenticated"]+ },+ "email": {+ "type": "string",+ "x-faker": "internet.email"+ },+ "phone": {+ "type": "string",+ "x-faker": {+ "fake": "{{phone.phoneNumber('+1##########')}}"+ }+ },+ "app_metadata": {+ "type": "object",+ "properties": {+ "provider": {+ "type": "string",+ "enum": ["email"]+ },+ "providers": {+ "type": "array",+ "items": {+ "type": "string",+ "enum": ["email"]+ },+ "minItems": 1,+ "maxItems": 1+ }+ }+ },+ "user_metadata": {+ "type": "object",+ "properties": {+ "email": {+ "type": "string",+ "x-faker": "internet.email"+ },+ "email_verified": {+ "type": "boolean",+ "x-faker": "random.boolean"+ },+ "phone_verified": {+ "type": "boolean",+ "x-faker": "random.boolean"+ },+ "sub": {+ "type": "string",+ "x-faker": "random.uuid"+ }+ }+ },+ "identities": {+ "type": "array",+ "items": {+ "type": "object",+ "properties": {+ "identity_id": {+ "type": "string",+ "x-faker": "random.uuid"+ },+ "id": {+ "type": "string",+ "x-faker": "random.uuid"+ },+ "user_id": {+ "type": "string",+ "x-faker": "random.uuid"+ },+ "identity_data": {+ "type": "object",+ "properties": {+ "email": {+ "type": "string",+ "x-faker": "internet.email"+ },+ "email_verified": {+ "type": "boolean",+ "x-faker": "random.boolean"+ },+ "phone_verified": {+ "type": "boolean",+ "x-faker": "random.boolean"+ },+ "sub": {+ "type": "string",+ "x-faker": "random.uuid"+ }+ }+ },+ "provider": {+ "type": "string",+ "enum": ["email"]+ },+ "last_sign_in_at": {+ "type": "string",+ "format": "date-time",+ "x-faker": "date.recent"+ },+ "created_at": {+ "type": "string",+ "format": "date-time",+ "x-faker": "date.recent"+ },+ "updated_at": {+ "type": "string",+ "format": "date-time",+ "x-faker": "date.recent"+ },+ "email": {+ "type": "string",+ "x-faker": "internet.email"+ }+ },+ "required": [+ "identity_id",+ "id",+ "user_id",+ "identity_data",+ "provider",+ "last_sign_in_at",+ "created_at",+ "updated_at",+ "email"+ ]+ }+ },+ "created_at": {+ "type": "string",+ "format": "date-time",+ "x-faker": "date.recent"+ },+ "updated_at": {+ "type": "string",+ "format": "date-time",+ "x-faker": "date.recent"+ },+ "is_anonymous": {+ "type": "boolean",+ "x-faker": "random.boolean"+ }+ },+ "required": [+ "id",+ "aud",+ "role",+ "email",+ "phone",+ "app_metadata",+ "user_metadata",+ "identities",+ "created_at",+ "updated_at",+ "is_anonymous"+ ]+ },+ "email_data": {+ "type": "object",+ "properties": {+ "token": {+ "type": "string",+ "pattern": "^[0-9]{6}$",+ "x-faker": {+ "fake": "{{helpers.replaceSymbols('######')}}"+ }+ },+ "token_hash": {+ "type": "string",+ "minLength": 16,+ "maxLength": 30,+ "x-faker": {+ "fake": "{{random.alphaNumeric(30)}}"+ }+ },+ "redirect_to": {+ "type": "string",+ "x-faker": "internet.url"+ },+ "email_action_type": {+ "type": "string",+ "enum": ["signup", "login", "reset_password"]+ },+ "site_url": {+ "type": "string",+ "x-faker": "internet.url"+ },+ "token_new": {+ "type": "string",+ "minLength": 16,+ "maxLength": 30,+ "x-faker": {+ "fake": "{{random.alphaNumeric(30)}}"+ }+ },+ "token_hash_new": {+ "type": "string",+ "minLength": 16,+ "maxLength": 30,+ "x-faker": {+ "fake": "{{random.alphaNumeric(30)}}"+ }+ }+ },+ "required": [+ "token",+ "token_hash",+ "redirect_to",+ "email_action_type",+ "site_url",+ "token_new",+ "token_hash_new"+ ]+ }+ },+ "required": ["user", "email_data"]+}+```++</TabPanel>+</Tabs>++**Outputs**++- No outputs are required. An empty response with a status code of 200 is taken as a successful response.++<Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="sql"+ queryGroup="language"+>+<TabPanel id="sql" label="SQL">+<Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="sql-queue-email-messages"+>+<TabPanel id="sql-queue-email-messages" label="Queue Email Messages">+Your company uses a worker to manage all emails related jobs. For performance reasons, the messaging system sends emails in batches via a job queue. Instead of sending a message immediately, messages are queued and sent in periodic intervals via `pg_cron`.++Create a table to store jobs++```sql+create table job_queue (+ job_id uuid primary key default gen_random_uuid(),+ job_data jsonb not null,+ created_at timestamp default now(),+ status text default 'pending',+ priority int default 0,+ retry_count int default 0,+ max_retries int default 2,+ scheduled_at timestamp default now()+);+```++Create the hook++```sql+create or replace function send_email(event jsonb) returns void as $$+declare+ job_data jsonb;+ scheduled_time timestamp;+ priority int;+begin+ -- Extract email details from the event JSON+ job_data := jsonb_build_object(+ 'email_action_type', event->'email_data'->>'email_action_type',+ 'token_hash', event->'email_data'->>'token_hash',+ 'token', event->'email_data'->>'token',+ 'email', event->'user'->>'email'+ );++ -- Calculate the nearest 5-minute window for scheduled_time+ scheduled_time := date_trunc('minute', now()) + interval '5 minute' * floor(extract('epoch' from (now() - date_trunc('minute', now())) / 60) / 5);++ -- Assign priority dynamically (example logic: higher priority for earlier scheduled time)+ priority := extract('epoch' from (scheduled_time - now()))::int;++ insert into job_queue (job_data, priority, scheduled_at, max_retries)+ values (job_data, priority, scheduled_time, 2);+end;+$$ language plpgsql;++grant execute+ on function public.send_email+ to supabase_auth_admin;++revoke execute+ on function public.send_email+ from authenticated, anon;++grant all+ on table public.job_queue+ to supabase_auth_admin;++revoke all+ on table public.job_queue+ from authenticated, anon;+```++Create a function to periodically run and dequeue all jobs++```sql+create or replace function dequeue_and_run_jobs() returns void as $$+declare+ job record;+begin+ for job in+ select * from job_queue+ where status = 'pending'+ and scheduled_at <= now()+ order by priority desc, created_at+ for update skip locked+ loop+ begin+ -- add job processing logic here.+ -- for demonstration, we'll just update the job status to 'completed'.+ update job_queue+ set status = 'completed'+ where job_id = job.job_id;++ exception when others then+ -- handle job failure and retry logic+ if job.retry_count < job.max_retries then+ update job_queue+ set retry_count = retry_count + 1,+ scheduled_at = now() + interval '1 minute' -- delay retry by 1 minute+ where job_id = job.job_id;+ else+ update job_queue+ set status = 'failed'+ where job_id = job.job_id;+ end if;+ end;+ end loop;+end;+$$ language plpgsql;++grant execute+ on function public.dequeue_and_run_jobs+ to supabase_auth_admin;++revoke execute+ on function public.dequeue_and_run_jobs+ from authenticated, anon;+```++Configure `pg_cron` to run the job on an interval. You can use a tool like [crontab.guru](https://crontab.guru/) to check that your job is running on an appropriate schedule. Ensure that `pg_cron` is enabled under `Database > Extensions`++```sql+select+ cron.schedule(+ '* * * * *', -- this cron expression means every minute.+ 'select dequeue_and_run_jobs();'+ );+```++</TabPanel>+</Tabs>+</TabPanel>+<TabPanel id="http" label="HTTP">+<Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="http-internationalization-for-emails"+>+<TabPanel id="http-internationalization-for-emails" label="Add Internationalization for Email Templates">+Your company is expanding to France and Spain. As part of expansion efforts, the company would like to deliver internationalized email templates to best support local users in their native language. Ensure that you have configured `POSTMARK_SERVER_TOKEN` and `AUTH_SEND_EMAIL_HOOK_SECRET` in your `.env` file.++```javascript+import { Webhook } from 'https://esm.sh/[email protected]'+import { readAll } from 'https://deno.land/std/io/read_all.ts'++const postmarkEndpoint = 'https://api.postmarkapp.com/email'+// Replace this with your email+const FROM_EMAIL = '[email protected]'++// Email Subjects+const subjects = {+ en: {+ signup: 'Confirm Your Email',+ recovery: 'Reset Your Password',+ invite: 'You have been invited',+ magic_link: 'Your Magic Link',+ email_change: 'Confirm Email Change',+ email_change_new: 'Confirm New Email Address',+ reauthentication: 'Confirm Reauthentication',+ },+ es: {+ signup: 'Confirma tu correo electrónico',+ recovery: 'Restablece tu contraseña',+ invite: 'Has sido invitado',+ magic_link: 'Tu enlace mágico',+ email_change: 'Confirma el cambio de correo electrónico',+ email_change_new: 'Confirma la Nueva Dirección de Correo',+ reauthentication: 'Confirma la reautenticación',+ },+ fr: {+ signup: 'Confirmez votre adresse e-mail',+ recovery: 'Réinitialisez votre mot de passe',+ invite: 'Vous avez été invité',+ magic_link: 'Votre Lien Magique',+ email_change: 'Confirmez le changement d’adresse e-mail',+ email_change_new: 'Confirmez la nouvelle adresse e-mail',+ reauthentication: 'Confirmez la réauthentification',+ },+}++// HTML Body+const templates = {+ en: {+ signup: `<h2>Confirm your email</h2><p>Follow this link to confirm your email:</p><p><a href="{{confirmation_url}}">Confirm your email address</a></p><p>Alternatively, enter the code: {{token}}</p>`,+ recovery: `<h2>Reset password</h2><p>Follow this link to reset the password for your user:</p><p><a href="{{confirmation_url}}">Reset password</a></p><p>Alternatively, enter the code: {{token}}</p>`,+ invite: `<h2>You have been invited</h2><p>You have been invited to create a user on {{site_url}}. Follow this link to accept the invite:</p><p><a href="{{confirmation_url}}">Accept the invite</a></p><p>Alternatively, enter the code: {{token}}</p>`,+ magic_link: `<h2>Magic Link</h2><p>Follow this link to login:</p><p><a href="{{confirmation_url}}">Log In</a></p><p>Alternatively, enter the code: {{token}}</p>`,+ email_change: `<h2>Confirm email address change</h2><p>Follow this link to confirm the update of your email address from {{old_email}} to {{new_email}}:</p><p><a href="{{confirmation_url}}">Change email address</a></p><p>Alternatively, enter the codes: {{token}} and {{new_token}}</p>`,+ email_change_new: `<h2>Confirm New Email Address</h2><p>Follow this link to confirm your new email address:</p><p><a href="{{confirmation_url}}">Confirm new email address</a></p><p>Alternatively, enter the code: {{new_token}}</p>`,+ reauthentication: `<h2>Confirm reauthentication</h2><p>Enter the code: {{token}}</p>`,+ },+ es: {+ signup: `<h2>Confirma tu correo electrónico</h2><p>Sigue este enlace para confirmar tu correo electrónico:</p><p><a href="{{confirmation_url}}">Confirma tu correo electrónico</a></p><p>Alternativamente, ingresa el código: {{token}}</p>`,+ recovery: `<h2>Restablece tu contraseña</h2><p>Sigue este enlace para restablecer la contraseña de tu usuario:</p><p><a href="{{confirmation_url}}">Restablece tu contraseña</a></p><p>Alternativamente, ingresa el código: {{token}}</p>`,+ invite: `<h2>Has sido invitado</h2><p>Has sido invitado para crear un usuario en {{site_url}}. Sigue este enlace para aceptar la invitación:</p><p><a href="{{confirmation_url}}">Aceptar la invitación</a></p><p>Alternativamente, ingresa el código: {{token}}</p>`,+ magic_link: `<h2>Tu enlace mágico</h2><p>Sigue este enlace para iniciar sesión:</p><p><a href="{{confirmation_url}}">Iniciar sesión</a></p><p>Alternativamente, ingresa el código: {{token}}</p>`,+ email_change: `<h2>Confirma el cambio de correo electrónico</h2><p>Sigue este enlace para confirmar la actualización de tu correo electrónico de {{old_email}} a {{new_email}}:</p><p><a href="{{confirmation_url}}">Cambiar correo electrónico</a></p><p>Alternativamente, ingresa los códigos: {{token}} y {{new_token}}</p>`,+ email_change_new: `<h2>Confirma la Nueva Dirección de Correo</h2><p>Sigue este enlace para confirmar tu nueva dirección de correo electrónico:</p><p><a href="{{confirmation_url}}">Confirma la nueva dirección de correo</a></p><p>Alternativamente, ingresa el código: {{new_token}}</p>`,+ reauthentication: `<h2>Confirma la reautenticación</h2><p>Ingresa el código: {{token}}</p>`,+ },+ fr: {+ signup: `<h2>Confirmez votre adresse e-mail</h2><p>Suivez ce lien pour confirmer votre adresse e-mail :</p><p><a href="{{confirmation_url}}">Confirmez votre adresse e-mail</a></p><p>Vous pouvez aussi saisir le code : {{token}}</p>`,+ recovery: `<h2>Réinitialisez votre mot de passe</h2><p>Suivez ce lien pour réinitialiser votre mot de passe :</p><p><a href="{{confirmation_url}}">Réinitialisez votre mot de passe</a></p><p>Vous pouvez aussi saisir le code : {{token}}</p>`,+ invite: `<h2>Vous avez été invité</h2><p>Vous avez été invité à créer un utilisateur sur {{site_url}}. Suivez ce lien pour accepter l'invitation :</p><p><a href="{{confirmation_url}}">Acceptez l'invitation</a></p><p>Vous pouvez aussi saisir le code : {{token}}</p>`,+ magic_link: `<h2>Votre Lien Magique</h2><p>Suivez ce lien pour vous connecter :</p><p><a href="{{confirmation_url}}">Connectez-vous</a></p><p>Vous pouvez aussi saisir le code : {{token}}</p>`,+ email_change: `<h2>Confirmez le changement d’adresse e-mail</h2><p>Suivez ce lien pour confirmer la mise à jour de votre adresse e-mail de {{old_email}} à {{new_email}} :</p><p><a href="{{confirmation_url}}">Changez d’adresse e-mail</a></p><p>Vous pouvez aussi saisir les codes : {{token}} et {{new_token}}</p>`,+ email_change_new: `<h2>Confirmez la nouvelle adresse e-mail</h2><p>Suivez ce lien pour confirmer votre nouvelle adresse e-mail :</p><p><a href="{{confirmation_url}}">Confirmez la nouvelle adresse e-mail</a></p><p>Vous pouvez aussi saisir le code : {{new_token}}</p>`,+ reauthentication: `<h2>Confirmez la réauthentification</h2><p>Saisissez le code : {{token}}</p>`,+ },+}++Deno.serve(async (req) => {+ const payload = await req.text()+ const serverToken = Deno.env.get('POSTMARK_SERVER_TOKEN')+ const headers = Object.fromEntries(req.headers)+ const base64_secret = Deno.env.get('AUTH_SEND_EMAIL_HOOK_SECRET')+ const wh = new Webhook(base64_secret)+ const { user, email_data } = wh.verify(payload, headers)++ const language = (user.user_metadata && user.user_metadata.i18n) || 'en'+ const subject = subjects[language][email_data.email_action_type] || 'Notification'++ let template = templates[language][email_data.email_action_type]+ let htmlBody = template+ .replace('{{confirmation_url}}', email_data.confirmation_url)+ .replace('{{token}}', email_data.token || '')+ .replace('{{new_token}}', email_data.new_token || '')+ .replace('{{site_url}}', email_data.site_url || '')+ .replace('{{old_email}}', email_data.email || '')+ .replace('{{new_email}}', email_data.new_email || '')++ const requestOptions = {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json',+ Accept: 'application/json',+ 'X-Postmark-Server-Token': serverToken,+ },+ body: JSON.stringify({+ From: FROM_EMAIL,+ To: user.email,+ Subject: subject,+ HtmlBody: htmlBody,+ }),+ }++ try {+ const response = await fetch(postmarkEndpoint, requestOptions)+ if (!response.ok) {+ const errorData = await response.json()+ throw new Error(`Failed to send email: ${errorData.Message}`)+ }+ return new Response(+ JSON.stringify({+ message: 'Email sent successfully.',+ }),+ {+ headers: {+ 'Content-Type': 'application/json',+ },+ }+ )+ } catch (error) {+ return new Response(+ JSON.stringify({+ error: `Failed to process the request: ${error.message}`,+ }),+ {+ status: 500,+ headers: {+ 'Content-Type': 'application/json',+ },+ }+ )+ }+})+```++</TabPanel>+<TabPanel id="http-backup-email-provider" label="Add Backup Email Provider">+Your company is rapidly growing and depends heavily on email signups. You'd like to configure a backup email provider in case the email provider runs out of credits during your new product launch. Postmark and Sengrid are used as examples but in practice you can use any email provider.+Ensure that you have configured `POSTMARK_SERVER_TOKEN`, `SENDGRID_API_KEY` and `AUTH_SEND_EMAIL_HOOK_SECRET` in your `.env` file.++```javascript+import {+ Webhook+} from "https://esm.sh/[email protected]";+import {+ readAll+} from "https://deno.land/std/io/read_all.ts";++const postmarkEndpoint = 'https://api.postmarkapp.com/email';+const sendGridEndpoint = 'https://api.sendgrid.com/v3/mail/send';+const FROM_EMAIL = '[email protected]'++// Email Subjects+const subjects = {+ signup: 'Confirm Your Email',+ recovery: 'Reset Your Password',+ invite: 'You have been invited',+ magic_link: 'Your Magic Link',+ email_change: 'Confirm Email Change',+ email_change_new: 'Confirm New Email Address',+ reauthentication: 'Confirm Reauthentication'+};++// HTML Body+const templates = {+ signup: `<h2>Confirm your email</h2><p>Follow this link to confirm your email:</p><p><a href="{{confirmation_url}}">Confirm your email address</a></p><p>Alternatively, enter the code: {{token}}</p>`,+ recovery: `<h2>Reset password</h2><p>Follow this link to reset the password for your user:</p><p><a href="{{confirmation_url}}">Reset password</a></p><p>Alternatively, enter the code: {{token}}</p>`,+ invite: `<h2>You have been invited</h2><p>You have been invited to create a user on {{site_url}}. Follow this link to accept the invite:</p><p><a href="{{confirmation_url}}">Accept the invite</a></p><p>Alternatively, enter the code: {{token}}</p>`,+ magic_link: `<h2>Magic Link</h2><p>Follow this link to login:</p><p><a href="{{confirmation_url}}">Log In</a></p><p>Alternatively, enter the code: {{token}}</p>`,+ email_change: `<h2>Confirm email address change</h2><p>Follow this link to confirm the update of your email address from {{old_email}} to {{new_email}}:</p><p><a href="{{confirmation_url}}">Change email address</a></p><p>Alternatively, enter the codes: {{token}} and {{new_token}}</p>`,+ email_change_new: `<h2>Confirm New Email Address</h2><p>Follow this link to confirm your new email address:</p><p><a href="{{confirmation_url}}">Confirm new email address</a></p><p>Alternatively, enter the code: {{new_token}}</p>`,+ reauthentication: `<h2>Confirm reauthentication</h2><p>Enter the code: {{token}}</p>`+};++async function sendEmailWithPostmark(user: any, email_data: any, serverToken: string): Promise<Response> {+ const subject = subjects[email_data.email_action_type] || 'Notification';+ let template = templates[email_data.email_action_type];+ let htmlBody = template.replace('{{confirmation_url}}', email_data.confirmation_url)+ .replace('{{token}}', email_data.token || '')+ .replace('{{new_token}}', email_data.new_token || '')+ .replace('{{site_url}}', email_data.site_url || '')+ .replace('{{old_email}}', email_data.email || '')+ .replace('{{new_email}}', email_data.new_email || '');++ const requestOptions = {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json',+ 'Accept': 'application/json',+ 'X-Postmark-Server-Token': serverToken+ },+ body: JSON.stringify({+ From: FROM_EMAIL,+ To: user.email,+ Subject: subject,+ HtmlBody: htmlBody+ })+ };++ return await fetch(postmarkEndpoint, requestOptions);+}++async function sendEmailWithSendGrid(user: any, email_data: any, apiKey: string): Promise<Response> {+ const subject = subjects[email_data.email_action_type] || 'Notification';+ let template = templates[email_data.email_action_type];+ let htmlBody = template.replace('{{confirmation_url}}', email_data.confirmation_url)+ .replace('{{token}}', email_data.token || '')+ .replace('{{new_token}}', email_data.new_token || '')+ .replace('{{site_url}}', email_data.site_url || '')+ .replace('{{old_email}}', email_data.email || '')+ .replace('{{new_email}}', email_data.new_email || '');++ const requestOptions = {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json',+ 'Authorization': `Bearer ${apiKey}`+ },+ body: JSON.stringify({+ personalizations: [{+ to: [{+ email: user.email+ }],+ subject: subject+ }],+ from: {+ email: FROM_EMAIL+ },+ content: [{+ type: "text/html",+ value: htmlBody+ }]+ })+ };++ return await fetch(sendGridEndpoint, requestOptions);+}++Deno.serve(async (req) => {+ const payload = await req.text();+ const postmarkServerToken = Deno.env.get("POSTMARK_SERVER_TOKEN");+ const sendGridApiKey = Deno.env.get("SENDGRID_API_KEY");+ const headers = Object.fromEntries(req.headers);+ const base64_secret = Deno.env.get('AUTH_SEND_EMAIL_HOOK_SECRET');+ const wh = new Webhook(base64_secret);+ const {+ user,+ email_data+ } = wh.verify(payload, headers);++ try {+ // Try sending email using Postmark+ let response = await sendEmailWithPostmark(user, email_data, postmarkServerToken!);++ if (!response.ok) {+ // If Postmark fails, try SendGrid+ console.error(`Primary email send failed: ${await response.text()}`);+ response = await sendEmailWithSendGrid(user, email_data, sendGridApiKey!);++ if (!response.ok) {+ const errorData = await response.json();+ throw new Error(`Failed to send email via backup: ${errorData.errors[0].message}`);+ }+ }++ return new Response(JSON.stringify({+ message: "Email sent successfully."+ }), {+ headers: {+ "Content-Type": "application/json"+ }+ });+ } catch (error) {+ return new Response(JSON.stringify({+ error: `Failed to process the request: ${error.message}`+ }), {+ status: 500,+ headers: {+ "Content-Type": "application/json"+ }+ });+ }+});+```++</TabPanel>+</Tabs>+</TabPanel>+</Tabs>diff --git a/apps/docs/content/guides/auth/auth-hooks/send-sms-hook.mdx b/apps/docs/content/guides/auth/auth-hooks/send-sms-hook.mdxnew file mode 100644index 00000000000..96439bd6452--- /dev/null+++ b/apps/docs/content/guides/auth/auth-hooks/send-sms-hook.mdx@@ -0,0 +1,653 @@+---+id: 'send-sms-hook'+title: 'Send SMS Hook'+subtitle: 'Use a custom SMS provider to send authentication messages'+---++Runs before a message is sent. Use the hook to:++- Use a regional SMS Provider+- Use alternate messaging channels such as WhatsApp+- Adjust the message body to include platform specific fields such as the [AppHash](https://developers.google.com/identity/sms-retriever/overview)++**Inputs**++| Field | Type | Description |+| ------ | ------------------------------------------------- | --------------------------------------------------------------- |+| `user` | [`User`](/docs/guides/auth/users#the-user-object) | The user attempting to sign in. |+| `sms` | `object` | Metadata specific to the SMS sending process. Includes the OTP. |++<Tabs+ scrollable+ size="small"+ type="underlined"+>+<TabPanel id="send-sms-json" label="JSON">++```json+{+ "user": {+ "id": "6481a5c1-3d37-4a56-9f6a-bee08c554965",+ "aud": "authenticated",+ "role": "authenticated",+ "email": "",+ "phone": "+1333363128",+ "phone_confirmed_at": "2024-05-13T11:52:48.157306Z",+ "confirmation_sent_at": "2024-05-14T12:31:52.824573Z",+ "confirmed_at": "2024-05-13T11:52:48.157306Z",+ "phone_change_sent_at": "2024-05-13T11:47:02.183064Z",+ "last_sign_in_at": "2024-05-13T11:52:48.162518Z",+ "app_metadata": {+ "provider": "phone",+ "providers": ["phone"]+ },+ "user_metadata": {},+ "identities": [+ {+ "identity_id": "3be5e552-65aa-41d9-9db9-2a502f845459",+ "id": "6481a5c1-3d37-4a56-9f6a-bee08c554965",+ "user_id": "6481a5c1-3d37-4a56-9f6a-bee08c554965",+ "identity_data": {+ "email_verified": false,+ "phone": "+1612341244428",+ "phone_verified": true,+ "sub": "6481a5c1-3d37-4a56-9f6a-bee08c554965"+ },+ "provider": "phone",+ "last_sign_in_at": "2024-05-13T11:52:48.155562Z",+ "created_at": "2024-05-13T11:52:48.155599Z",+ "updated_at": "2024-05-13T11:52:48.159391Z"+ }+ ],+ "created_at": "2024-05-13T11:45:33.7738Z",+ "updated_at": "2024-05-14T12:31:52.82475Z",+ "is_anonymous": false+ },+ "sms": {+ "otp": "561166"+ }+}+```++</TabPanel>+<TabPanel id="send-sms-json-schema" label="JSON Schema">++```json+{+ "type": "object",+ "properties": {+ "user": {+ "type": "object",+ "properties": {+ "id": {+ "type": "string",+ "x-faker": "random.uuid"+ },+ "aud": {+ "type": "string",+ "enum": ["authenticated"]+ },+ "role": {+ "type": "string",+ "enum": ["anon", "authenticated"]+ },+ "email": {+ "type": "string",+ "x-faker": "internet.email"+ },+ "phone": {+ "type": "string",+ "x-faker": {+ "fake": "{{phone.phoneNumber('+1##########')}}"+ }+ },+ "phone_confirmed_at": {+ "type": "string",+ "format": "date-time",+ "x-faker": "date.recent"+ },+ "confirmation_sent_at": {+ "type": "string",+ "format": "date-time",+ "x-faker": "date.recent"+ },+ "confirmed_at": {+ "type": "string",+ "format": "date-time",+ "x-faker": "date.recent"+ },+ "phone_change_sent_at": {+ "type": "string",+ "format": "date-time",+ "x-faker": "date.recent"+ },+ "last_sign_in_at": {+ "type": "string",+ "format": "date-time",+ "x-faker": "date.recent"+ },+ "app_metadata": {+ "type": "object",+ "properties": {+ "provider": {+ "type": "string",+ "enum": ["phone"]+ },+ "providers": {+ "type": "array",+ "items": {+ "type": "string",+ "enum": ["phone"]+ }+ }+ }+ },+ "user_metadata": {+ "type": "object",+ "x-faker": "random.objectElement"+ },+ "identities": {+ "type": "array",+ "items": {+ "type": "object",+ "properties": {+ "identity_id": {+ "type": "string",+ "x-faker": "random.uuid"+ },+ "id": {+ "type": "string",+ "x-faker": "random.uuid"+ },+ "user_id": {+ "type": "string",+ "x-faker": "random.uuid"+ },+ "identity_data": {+ "type": "object",+ "properties": {+ "email_verified": {+ "type": "boolean",+ "x-faker": "random.boolean"+ },+ "phone": {+ "type": "string",+ "x-faker": {+ "fake": "{{phone.phoneNumber('+1##########')}}"+ }+ },+ "phone_verified": {+ "type": "boolean",+ "x-faker": "random.boolean"+ },+ "sub": {+ "type": "string",+ "x-faker": "random.uuid"+ }+ }+ },+ "provider": {+ "type": "string",+ "enum": ["phone", "email", "google"]+ },+ "last_sign_in_at": {+ "type": "string",+ "format": "date-time",+ "x-faker": "date.recent"+ },+ "created_at": {+ "type": "string",+ "format": "date-time",+ "x-faker": "date.recent"+ },+ "updated_at": {+ "type": "string",+ "format": "date-time",+ "x-faker": "date.recent"+ }+ },+ "required": [+ "identity_id",+ "id",+ "user_id",+ "identity_data",+ "provider",+ "last_sign_in_at",+ "created_at",+ "updated_at"+ ]+ }+ },+ "created_at": {+ "type": "string",+ "format": "date-time",+ "x-faker": "date.recent"+ },+ "updated_at": {+ "type": "string",+ "format": "date-time",+ "x-faker": "date.recent"+ },+ "is_anonymous": {+ "type": "boolean",+ "x-faker": "random.boolean"+ }+ },+ "required": [+ "id",+ "aud",+ "role",+ "email",+ "phone",+ "phone_confirmed_at",+ "confirmation_sent_at",+ "confirmed_at",+ "phone_change_sent_at",+ "last_sign_in_at",+ "app_metadata",+ "user_metadata",+ "identities",+ "created_at",+ "updated_at",+ "is_anonymous"+ ]+ },+ "sms": {+ "type": "object",+ "properties": {+ "otp": {+ "type": "string",+ "pattern": "^[0-9]{6}$",+ "x-faker": {+ "fake": "{{helpers.replaceSymbols(######)}}"+ }+ }+ },+ "required": ["otp"]+ }+ },+ "required": ["user", "sms"]+}+```++</TabPanel>+</Tabs>++**Outputs**++- No outputs are required. An empty response with a status code of 200 is taken as a successful response.++<Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="sql"+ queryGroup="language"+>+<TabPanel id="sql" label="SQL">+<Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="sql-send-message-via-job-queue"+>+<TabPanel id="sql-send-message-via-job-queue" label="Queue SMS Messages">++Your company uses a worker to manage all messaging related jobs. For performance reasons, the messaging system sends messages in intervals via a job queue. Instead of sending a message immediately, messages are queued and sent in periodic intervals via `pg_cron`.++Create a table to store jobs++```sql+create table job_queue (+ job_id uuid primary key default gen_random_uuid(),+ job_data jsonb not null,+ created_at timestamp default now(),+ status text default 'pending',+ priority int default 0,+ retry_count int default 0,+ max_retries int default 2,+ scheduled_at timestamp default now()+);+```++Create the hook:++```sql+create or replace function send_sms(event jsonb) returns void as $$+declare+ job_data jsonb;+ scheduled_time timestamp;+ priority int;+begin+ -- extract phone and otp from the event json+ job_data := jsonb_build_object(+ 'phone', event->'user'->>'phone',+ 'otp', event->'sms'->>'otp'+ );++ -- calculate the nearest 5-minute window for scheduled_time+ scheduled_time := date_trunc('minute', now()) + interval '5 minute' * floor(extract('epoch' from (now() - date_trunc('minute', now())) / 60) / 5);++ -- assign priority dynamically (example logic: higher priority for earlier scheduled time)+ priority := extract('epoch' from (scheduled_time - now()))::int;++ -- insert the job into the job_queue table+ insert into job_queue (job_data, priority, scheduled_at, max_retries)+ values (job_data, priority, scheduled_time, 2);+end;+$$ language plpgsql;++grant execute+ on function public.send_sms+ to supabase_auth_admin;++revoke execute+ on function public.send_sms+ from authenticated, anon;++grant all+ on table public.job_queue+ to supabase_auth_admin;++revoke all+ on table public.job_queue+ from authenticated, anon;+```++Create a function to periodically run and dequeue all jobs++```sql+create or replace function dequeue_and_run_jobs() returns void as $$+declare+ job record;+begin+ for job in+ select * from job_queue+ where status = 'pending'+ and scheduled_at <= now()+ order by priority desc, created_at+ for update skip locked+ loop+ begin+ -- add job processing logic here.+ -- for demonstration, we'll just update the job status to 'completed'.+ update job_queue+ set status = 'completed'+ where job_id = job.job_id;++ exception when others then+ -- handle job failure and retry logic+ if job.retry_count < job.max_retries then+ update job_queue+ set retry_count = retry_count + 1,+ scheduled_at = now() + interval '1 minute' -- delay retry by 1 minute+ where job_id = job.job_id;+ else+ update job_queue+ set status = 'failed'+ where job_id = job.job_id;+ end if;+ end;+ end loop;+end;+$$ language plpgsql;++grant execute+ on function public.dequeue_and_run_jobs+ to supabase_auth_admin;++revoke execute+ on function public.dequeue_and_run_jobs+ from authenticated, anon;+```++Configure `pg_cron` to run the job on an interval. You can use a tool like [crontab.guru](https://crontab.guru/) to check that your job is running on an appropriate schedule. Ensure that `pg_cron` is enabled under `Database > Extensions`++```sql+select+ cron.schedule(+ '* * * * *', -- this cron expression means every minute.+ 'select dequeue_and_run_jobs();'+ );+```++</TabPanel>+</Tabs>+</TabPanel>+<TabPanel id="http" label="HTTP">+<Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="http-alternate-message-provider"+>+<TabPanel id="http-alternate-message-provider" label="Alternate message provider">+Your company would like to use an alternate message provider. Some examples of alternate message providers include [Msg91](https://msg91.com/) for India and [Africa's Talking](https://africastalking.com/). The example uses Twilio as it is widely available and does not require a regional number.++```javascript+import { Webhook } from 'https://esm.sh/[email protected]'+import { readAll } from 'https://deno.land/std/io/read_all.ts'+import { Twilio } from 'https://cdn.skypack.dev/twilio'+import * as base64 from 'https://denopkg.com/chiefbiiko/base64/mod.ts'++const accountSid: string | undefined = Deno.env.get('TWILIO_ACCOUNT_SID')+const authToken: string | undefined = Deno.env.get('TWILIO_AUTH_TOKEN')+const fromNumber: string = Deno.env.get('TWILIO_PHONE_NUMBER')++const sendTextMessage = async (+ messageBody: string,+ accountSid: string | undefined,+ authToken: string | undefined,+ fromNumber: string,+ toNumber: string+): Promise<any> => {+ if (!accountSid || !authToken) {+ console.log('Your Twilio account credentials are missing. Please add them.')+ return+ }+ const url: string = `https://api.twilio.com/2010-04-01/Accounts/${accountSid}/Messages.json`++ const encodedCredentials: string = base64.fromUint8Array(+ new TextEncoder().encode(`${accountSid}:${authToken}`)+ )++ const body: URLSearchParams = new URLSearchParams({+ To: `+${toNumber}`,+ From: fromNumber,+ // Uncomment when testing with a fixed number+ Body: messageBody,+ })++ const response = await fetch(url, {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/x-www-form-urlencoded',+ Authorization: `Basic ${encodedCredentials}`,+ },+ body,+ })++ return response.json()+}++Deno.serve(async (req) => {+ const payload = await req.text()+ const base64_secret = Deno.env.get('SEND_SMS_HOOK_SECRET')+ const headers = Object.fromEntries(req.headers)+ const wh = new Webhook(base64_secret)+ try {+ const { user, sms } = wh.verify(payload, headers)+ const messageBody = `Your OTP is: ${sms.otp}`+ const response = await sendTextMessage(+ messageBody,+ accountSid,+ authToken,+ fromNumber,+ user.phone+ )+ if (response.status !== 'queued') {+ return new Response(+ JSON.stringify({+ error: {+ http_code: response.code,+ message: `Failed to send SMS: ${response.message}. More info: ${response.more_info}`,+ },+ }),+ {+ status: response.status,+ headers: {+ 'Content-Type': 'application/json',+ },+ }+ )+ }+ return new Response(+ {},+ {+ status: 200,+ headers: {+ 'Content-Type': 'application/json',+ },+ }+ )+ } catch (error) {+ return new Response(+ JSON.stringify({+ error: {+ http_code: 500,+ message: `Failed to send sms: ${JSON.stringify(error)}`,+ }+ }),+ {+ status: 500,+ headers: {+ 'Content-Type': 'application/json',+ },+ }+ )+ }+})+```++</TabPanel>+<TabPanel id="http-whatsapp-and-sms-messages" label="Use WhatsApp with SMS">++Your company is expanding into Latin America and would like to use WhatsApp for higher deliverability. Write a hook to send WhatsApp messages to requests from the continent and SMS messages to all other numbers.++```javascript+import {+ Webhook+} from "https://esm.sh/[email protected]";+import {+ readAll+} from "https://deno.land/std/io/read_all.ts";+import * as base64 from "https://denopkg.com/chiefbiiko/base64/mod.ts";++const accountSid: string | undefined = Deno.env.get("TWILIO_ACCOUNT_SID");+const authToken: string | undefined = Deno.env.get("TWILIO_AUTH_TOKEN");+const fromNumber: string = Deno.env.get("TWILIO_WHATSAPP_NUMBER");+const smsFromNumber: string = Deno.env.get("TWILIO_SMS_NUMBER");++const latinAmericanCountryCodes = ['54', '55', '56', '57', '58', '501', '502', '503', '504', '505', '506', '507', '508', '509', '51', '52', '53', '591', '592', '593', '594', '595', '596', '597', '598', '599'];++const sendMessage = async (+ messageBody: string,+ accountSid: string | undefined,+ authToken: string | undefined,+ fromNumber: string,+ toNumber: string,+ useWhatsApp: boolean,+): Promise < any > => {+ if (!accountSid || !authToken) {+ console.log("Your Twilio account credentials are missing. Please add them.");+ return;+ }+ const url: string = `https://api.twilio.com/2010-04-01/Accounts/${accountSid}/Messages.json`;++ const encodedCredentials: string = base64.fromUint8Array(+ new TextEncoder().encode(`${accountSid}:${authToken}`),+ );++ const body: URLSearchParams = new URLSearchParams({+ To: useWhatsApp ? `whatsapp:${toNumber}` : toNumber,+ From: useWhatsApp ? `whatsapp:${fromNumber}` : smsFromNumber,+ Body: messageBody,+ });++ const response = await fetch(url, {+ method: "POST",+ headers: {+ "Content-Type": "application/x-www-form-urlencoded",+ "Authorization": `Basic ${encodedCredentials}`,+ },+ body,+ });++ return response.json();+};++Deno.serve(async (req) => {+ const payload = await req.text();+ const base64_secret = Deno.env.get("SEND_SMS_HOOK_SECRET");+ const headers = Object.fromEntries(req.headers);+ const wh = new Webhook(base64_secret);+ try {+ const {+ user,+ sms+ } = wh.verify(payload, headers);+ const messageBody = `Your OTP is: ${sms.otp}`;+ const userPhoneNumber = user.phone;+ const countryCode = userPhoneNumber.substring(1, userPhoneNumber.indexOf(userPhoneNumber.match(/\d/)!));++ const useWhatsApp = latinAmericanCountryCodes.includes(countryCode);++ const response = await sendMessage(+ messageBody,+ accountSid,+ authToken,+ fromNumber,+ userPhoneNumber,+ useWhatsApp,+ );++ if (response.status !== "queued") {+ return new Response(+ JSON.stringify({+ error: `Failed to send message, Error Code: ${response.code} ${response.message} ${response.more_info}`,+ }), {+ status: response.status,+ headers: {+ "Content-Type": "application/json",+ },+ },+ );+ }+ return new Response(+ JSON.stringify({+ message: "Message sent successfully."+ }), {+ headers: {+ "Content-Type": "application/json",+ },+ },+ );+ } catch (error) {+ return new Response(+ JSON.stringify({+ error: `Failed to process the request: ${error}`+ }), {+ status: 500,+ headers: {+ "Content-Type": "application/json",+ },+ },+ );+ }+});+```++</TabPanel>+</Tabs>+</TabPanel>+</Tabs>diff --git a/packages/shared-data/config.ts b/packages/shared-data/config.tsindex 1cdda69a3f0..220c6f74a91 100644--- a/packages/shared-data/config.ts+++ b/packages/shared-data/config.ts@@ -98,6 +98,15 @@ const config = {},},},+ hook_timeouts: {+ postgres_hooks: {+ value: 2,+ },+ http_hooks: {+ value: 5,++ },+ }},branching: {inactivity_period_in_minutes: {