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.
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.
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();- Replace orders with each table you want audited, one create trigger per table.
- 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.
- 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.
- Try to write to audit_log as that user (from the browser console or the app). The database should refuse with a permission error.
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.
| Value | Where it goes | Why |
|---|---|---|
| Stripe secret key, OpenAI key, webhook signing secret | Lovable Secrets, read in an Edge Function | Backend-only; never reaches the browser |
| Supabase URL and publishable (anon) key | .env as VITE_ variables | Built into the browser bundle; safe to expose because RLS limits them |
| Supabase secret or service_role key | Only in Edge Functions (Lovable provides it) | Bypasses every RLS policy |
| A secret a database function or trigger needs | Supabase Vault | Encrypted on disk, readable from SQL by roles you allow |
| Any key, if you connect your own external Supabase project | Supabase dashboard: Edge Function Secrets, or supabase secrets set | Lovable's docs send you there for external Supabase |
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.
- 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.
- 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.
- Exercise the feature that uses the key, such as a test payment or an AI call, and check the Edge Function logs for errors.
- Revoke the old key at the issuing service.
- Record the rotation in audit_log or your team notes, without the key itself.
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?
Can users delete or edit their own audit log entries?
Where do I store API keys in a Lovable app?
Is the Supabase anon or publishable key a secret?
Do I need to redeploy Edge Functions after changing a secret?
Will logging every change slow my Lovable app down?
Sources
- Lovable Docs — Secrets: where backend keys live, rotation, .env vs secrets (read 2026-11-17)
- Lovable Docs — security best practices: secrets in server-side code only, log important security events (read 2026-11-17)
- Supabase Docs — Auth audit logs (read 2026-11-17)
- PostgreSQL Docs — PL/pgSQL trigger functions, Example 41.4: a trigger function for auditing
- Supabase Docs — Row Level Security: security definer functions, search_path, exposed schemas (read 2026-11-17)
- Supabase Docs — Edge Function environment variables and secrets (read 2026-11-17)
- Supabase Docs — Vault: encrypted secrets for database functions (read 2026-11-17)
- Supabase Docs — API keys: publishable vs secret (read 2026-11-17)
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.