Hire Lovable Xperts
Security

How to Add Audit Logs and Secrets Management to a Lovable App

Add audit logs by creating an audit_log table in Supabase that a trigger fills on every insert, update and delete, with RLS on and no write access for app users, so nobody can edit the record. Manage secrets in Lovable's Secrets (or Supabase Edge Function secrets), read them only in Edge Functions, and rotate a key by issuing a new one, replacing the secret, then revoking the old key.

By Hire Lovable Xperts · Last verified: 2026-11-17

What does a Lovable app already log, and what's missing?

Supabase Auth already keeps an audit log of authentication events: sign-ups, sign-ins, password changes and resets, email verification, token refreshes and sign-outs, each with a timestamp, user ID, action and user agent. What nothing records by default is who changed your own data: which user edited an order, deleted a record or changed a role. That is what an audit_log table adds.

On a Supabase project you manage, the auth log is under Authentication → Audit Logs in the dashboard. Supabase can also write it to the auth.audit_log_entries table, searchable with SQL, at the cost of extra database storage. Lovable's own security guidance asks you to log important security events and to keep secret values out of those logs, which shapes the table below.

How do you create an audit log table users can't edit?

Create the table, enable Row Level Security, and give the app's roles no way to write to it: no insert, update or delete policy, and those privileges revoked from anon and authenticated. Only the trigger in the next step writes rows. Whether anyone reads it through the API is your choice; by default this setup lets you read it only in the dashboard or SQL editor.

SQL: the audit_log table (run in the SQL editor)
create table public.audit_log (
  id bigint generated always as identity primary key,
  occurred_at timestamptz not null default now(),
  actor_id uuid,          -- the signed-in user who made the change
  table_name text not null,
  operation text not null, -- INSERT, UPDATE or DELETE
  row_id text,
  old_data jsonb,
  new_data jsonb
);

alter table public.audit_log enable row level security;
-- No write policies, and no write privileges for the app's roles:
revoke insert, update, delete, truncate on public.audit_log from anon, authenticated;

If admins should read the log inside the app, add a select policy that checks an admin role through a security definer helper rather than a subquery on another policy-protected table; our guide to the RLS infinite recursion error shows that pattern.

How do you record every change with a trigger?

A row-level AFTER trigger runs once for each inserted, updated or deleted row and writes who, what, when, and the row before and after. It follows PostgreSQL's own auditing example. The function is security definer, so it can write to audit_log when the user can't, and it lives in a private schema with an empty search_path, as Supabase advises for such functions.

SQL: the trigger function and one trigger per audited table
create schema if not exists private;

create or replace function private.write_audit_log()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
begin
  insert into public.audit_log (actor_id, table_name, operation, row_id, old_data, new_data)
  values (
    auth.uid(),
    tg_table_name,
    tg_op,
    coalesce(to_jsonb(new) ->> 'id', to_jsonb(old) ->> 'id'),
    case when tg_op in ('UPDATE', 'DELETE') then to_jsonb(old) - 'api_token' end,
    case when tg_op in ('INSERT', 'UPDATE') then to_jsonb(new) - 'api_token' end
  );
  return null; -- AFTER trigger: the return value is ignored
end;
$$;

-- Repeat for each table you want audited:
create trigger audit_orders
  after insert or update or delete on public.orders
  for each row execute function private.write_audit_log();
  1. Replace orders with each table you want audited, one create trigger per table.
  2. Replace 'api_token' with any column that must never be copied into the log (tokens, password hashes, card data). Each - 'column' removes one key from the logged JSON.
  3. Test it: sign in as a normal user, change a row in the app, then check audit_log in the SQL editor. You should see the user's ID, the operation and the before and after values.
  4. Try to write to audit_log as that user (from the browser console or the app). The database should refuse with a permission error.
Changes made with a secret or service_role key, or in the SQL editor, carry no signed-in user, so actor_id is empty. That still tells you something: the change came from server-side code or a person with dashboard access, not from the app.

How do you log events that aren't table changes?

Some events matter without changing a row: a data export, an admin viewing a user's account, a failed payment webhook. Log those from an Edge Function, which runs on the server. Insert into audit_log with a server-side Supabase client built from the service role key that Lovable injects as a secret, and take the actor from the caller's verified token, never from the request body.

In Lovable's Secrets docs, Edge Functions read that key with Deno.env.get("SUPABASE_SERVICE_ROLE_KEY"). A client built with it bypasses Row Level Security, which is why it can write to audit_log when app users can't, and why it must never be sent to the browser. Keep secret values and full request bodies out of what you log.

Where should secrets live in a Lovable app?

In Lovable's Secrets, under More → Settings → Secrets, or added through the secure input Lovable shows in chat. Lovable's docs say secrets are encrypted, injected into your Edge Functions and never reach the browser, and nobody can view a saved value afterwards. The .env file is different: it holds only VITE_ values, which are built into the browser code and public.

Lovable's docs also warn not to add .env to .gitignore, because Lovable needs it committed for previews and builds. That is safe only while .env holds nothing but public VITE_ values. Any real secret that has ever been in .env or a VITE_ variable should be treated as exposed; our guide to leaked .env keys covers getting it out of the repository.

Where each kind of value belongs (Lovable and Supabase docs, read 2026-11-17)
ValueWhere it goesWhy
Stripe secret key, OpenAI key, webhook signing secretLovable Secrets, read in an Edge FunctionBackend-only; never reaches the browser
Supabase URL and publishable (anon) key.env as VITE_ variablesBuilt into the browser bundle; safe to expose because RLS limits them
Supabase secret or service_role keyOnly in Edge Functions (Lovable provides it)Bypasses every RLS policy
A secret a database function or trigger needsSupabase VaultEncrypted on disk, readable from SQL by roles you allow
Any key, if you connect your own external Supabase projectSupabase dashboard: Edge Function Secrets, or supabase secrets setLovable's docs send you there for external Supabase

Related: Move secrets from client code into Edge Functions

How do you rotate a key in a Lovable app?

Issue a new key at the service that owns it, replace the secret, check the feature still works, then revoke the old key. In Lovable, secrets can't be edited, only replaced or deleted, so you add the new value under the same name. Supabase's Edge Functions read a changed secret immediately, with no redeploy. Lovable's own LOVABLE_API_KEY has a one-click Rotate action.

  1. Create a new key in the issuing service's dashboard (Stripe, OpenAI and so on). If it allows two active keys, leave the old one on for now.
  2. Replace the secret: in Lovable, add the new value under the same name; on your own Supabase project, run supabase secrets set NAME=new-value or use the Edge Function Secrets page.
  3. Exercise the feature that uses the key, such as a test payment or an AI call, and check the Edge Function logs for errors.
  4. Revoke the old key at the issuing service.
  5. Record the rotation in audit_log or your team notes, without the key itself.
If a key has leaked, revoke it first and accept a few minutes of downtime: anyone who copied it can use it until it's revoked. Then clean it out of the repository.

What should you check regularly once this is in place?

Four checks keep both systems honest. Confirm audit_log still has no write policies and RLS is on; the free RLS policy checker flags any always-true policy. Compare the secrets list with what your code uses. Scan the auth audit log for unusual sign-ins. And, as Lovable's own guidance puts it, regularly review who has access to the workspace and project.

Paste your policies into the Lovable RLS policy checker after any change Lovable makes to your schema; AI edits are where an audit table most often gains a write policy nobody asked for. If you want a person to check all of it, our Lovable security audit reviews RLS on every table, secrets including git history, auth and public endpoints, and gives you a severity-ranked report.

Frequently asked questions

Does Lovable have built-in audit logs for my app's data?
Lovable's security guidance tells you to log important security events, but its docs don't describe an audit log of your app's data. Supabase Auth logs authentication events; a history of who changed which rows is something you add with an audit table and trigger.
Can users delete or edit their own audit log entries?
Not with this setup. RLS is on with no write policies, and insert, update, delete and truncate are revoked from the anon and authenticated roles, so the database refuses their writes. Only the security definer trigger and server-side code using the service role key can add rows.
Where do I store API keys in a Lovable app?
In Lovable's Secrets (More → Settings → Secrets) for backend keys such as Stripe or OpenAI, read in Edge Functions with Deno.env.get. The .env file is only for VITE_ values, which end up in the browser and must be safe to publish.
Is the Supabase anon or publishable key a secret?
No. Supabase's docs say publishable keys are safe to expose in web pages and apps, because Row Level Security limits what they can do. Secret keys and the legacy service_role key bypass RLS and must stay in server-side code.
Do I need to redeploy Edge Functions after changing a secret?
Not on Supabase: its docs say functions read a new production secret immediately. Creating or deleting production secrets in Supabase requires the Owner or Administrator role.
Will logging every change slow my Lovable app down?
Each audited write adds one insert into audit_log, inside the same transaction, so a bulk update logs one row per changed row. For most apps that cost is small; audit only the tables where a change history matters, and watch the table's size as it grows.

Talk to a senior engineer — not a salesperson.

Book a free 30-minute audit call. We'll diagnose what's wrong and tell you exactly what it costs to fix.

Or hire a Lovable developer for the whole job →

Book a free audit call