Foundations — security, auth, database, cloud & AI

The shared vocabulary behind every project in this course. Decide what your app needs before you prompt.

12 min read·7 parts

Before we build five tools that make daily work easier, we need one short lesson on the foundations underneath all of them. Every project after this uses some mix of the same five things: security, auth, database, cloud, and AI. Not every project needs every piece — and that choice matters more than the code you write.


Part 1 · Why start here

The hard part of building tools with AI is not the typing — Lovable will happily generate 500 lines of code from a one-sentence prompt. The hard part is knowing, before you prompt, what kind of app you're actually building.

  • Does everyone see the same data, or only their own?
  • Do you need users to log in, or is an email enough?
  • Is data public, or is a leak a real problem?
  • Do you need AI, or would a rule cover it?

The rest of this course is five real projects, each answering those questions differently. This lesson gives you the shared vocabulary so the projects read the same way in your head.


Part 2 · Security mindset

There is one rule that never bends: never trust the browser. Anything running in the user's tab — your React code, your API keys accidentally left in the bundle, the "isAdmin" flag you set in localStorage — is visible and editable to the person sitting at the screen.

BROWSER · UNTRUSTEDAnyone can readHTML, JS bundle,localStorage, network tabSERVER · TRUSTEDOnly your code runsSecrets, admin queries,auth checks, AI keysRLS
Two sides of every app. The line between them is where security lives.

The three real gates

  • RLS (Row-Level Security) — rules on the database itself. Even if someone crafts a malicious request, the database refuses to return rows they shouldn't see.
  • Server functions — code that only runs on the backend. This is where secret keys live and where you check "is this user allowed to do this?" before doing it.
  • Input validation — never insert what a user typed straight into a query, an email, or an AI prompt without checking length and shape first.
What is NOT security
Hiding a button. Redirecting away from /admin. Setting role: "admin" in localStorage. These are UX polish — they stop honest users from clicking the wrong thing. They do not stop anyone determined.

Part 3 · Auth — when you need it

Auth means "the app knows who this person is because they proved it — usually with a password, a Google account, or a magic link to their email." Auth is not free: it comes with sign-in flows, password resets, session expiry, and support tickets. So the first question is always: do we need it at all?

Do
No auth is fine when…
Data is public, or a submitter's identity is not sensitive (a public form, a survey, a slide generator anyone can try). You can still collect an email as a plain text field — you just don't verify it.
Do
Auth is required when…
Each user must only see their own data (personal tracker), or specific people have elevated access (admin dashboard, team tools). If a leak between two users would be bad, you need auth and RLS.
Email is not identity
A form field labeled "email" identifies nothing — anyone can type any address. If you need to prove the person owns that email, you need auth (password, magic link, or Google sign-in). We'll build a project with each pattern.

Part 4 · Database — the shape of your data

A database is a set of tables. A table is a spreadsheet with rules — column types, required fields, and (crucially) policies that say who can read or write each row.

Three things to decide, in order

  1. 1Shape

    What are the rows? One row per submission? Per user? Per activity? Sketch the columns before you prompt.

  2. 2Ownership

    Every row belongs to somebody or nobody. If it belongs to a user, add a user_id column. If it belongs to "the public", say so explicitly.

  3. 3Policies (RLS)

    For each of INSERT, SELECT, UPDATE, DELETE — who is allowed? Anonymous visitors, signed-in users, only the owner, only admins?

Roles live in their own table

When more than one person can do more than one thing (a normal user vs an admin), store roles in a dedicated user_roles table — never a column on the profile, and never localStorage. Check roles with a security-definer function like has_role(). We cover this deeply in Lovable Lesson 16.


Part 5 · Cloud — what the backend actually is

"Cloud" in Lovable means four services you can enable without setting up servers:

  • Database — Postgres. Your tables live here.
  • Auth — email/password, Google, magic links, plus session management done for you.
  • Storage — file buckets (private by default) for uploads, images, and generated files.
  • Server functions — small pieces of code that run on the backend, not in the browser. Where secrets and privileged logic live.
Secrets ≠ config
A publishable/anon key is fine in the bundle. A secret key (OpenAI, Stripe secret, a private webhook signer) is only readable by server functions via process.env. We covered this in Lovable Lesson 17 — every project below follows the same rule.

Part 6 · AI — when to reach for it

AI is not the answer to every feature. It's the answer when the input is messy, fuzzy, or open-ended and a rule would be impossible to write. Otherwise, a plain function is faster, cheaper, and predictable.

Do
Good AI use
Summarize a paragraph. Map "Total Sales ($)" and "Revenue MYR" as the same column. Turn a bullet list into slide content. Suggest tags for a note.
Avoid
Bad AI use
Adding two numbers. Filtering a list where the user already chose a category. Deciding who's an admin. Anything a boolean or a WHERE clause could do.

In this course we always call AI from a server function using the Lovable AI Gateway — never directly from the browser, never with the key in the bundle, and always with a request-length cap.


Part 7 · Start every project with a Power Prompt

Every project lesson opens with a Power Prompt — the five-block structure from the power-prompt course. Use it before you type anything into Lovable's chat.

Goal · Input · Layout · Features · Output. Five blocks. Fill each one in a sentence. Paste. The agent will nail it on the first try far more often than with "build me a thing that does the thing".

Foundations cheat sheet

  • Browser = untrusted. Server = trusted. RLS is the line between them.
  • No auth is a valid choice — as long as you accept that identity is unverified.
  • Roles live in user_roles, never on a profile column, never in localStorage.
  • Secrets read from process.env inside server functions. Never in src/.
  • AI is for fuzzy inputs. Rules beat AI for anything a WHERE clause could solve.
  • Every project starts with a Power Prompt, not a chat message.