Lesson 12: Build a newsletter app (crypto)
Embark on your journey to building production grade apps.
You've learned the pieces: auth, payments, performance, email, security. This lesson is where they all come together. One app, one capstone, every skill doing a job.
Everything you've built toward in the last few lessons plugs in here: the database and RLS you set up in the auth lesson, the payment flow from the payments lesson, the indexes and pagination from the performance lesson, the transactional emails from the notifications lesson, and the security mindset from the security lesson.
We're going to build a paid newsletter platform: a place where creators post articles, readers pay once with a crypto wallet to unlock access, and the right things happen automatically: email on new posts, the content behind a paywall, the subscription state verified on-chain.
Capstone rules. No new concepts. Each step reuses a skill from an earlier lesson. If you get stuck, the answer is in the lesson that introduced that piece. The new thing here is wiring it all together into one app.
What We're Building
A creator can:
- Sign in and write posts with a title, a cover image, and a body.
- Set a price in MON.
- See who has subscribed.
A reader can:
- Browse public post previews (title, cover, first paragraph).
- Connect their crypto wallet and pay once to unlock a creator.
- Read full posts after paying.
- Receive an email the moment the creator publishes a new post.
Every skill from the sophomore track is doing a job:
- Database and storage hold posts, subscriptions, and cover images.
- Authentication tells the app who's signed in and what they can read.
- Crypto wallet and on-chain payment record the subscription permanently and publicly.
- Email notifications alert subscribers when a new post drops.
- Performance keeps the post feed snappy as content grows.
- Security is the glue: RLS on the paywall, signature checks on the payment, validation on every input.
In this app, what does the blockchain store?
And what stays in the database?
Set Up the Project
We're scaffolding an app with authentication and a database wired up, because those are the base layers. You already have a Supabase project from earlier lessons. Reuse it.
Open your Repl and paste into the AI panel:
I'm building the capstone for my sophomore course: a paid
newsletter platform. Scaffold it:
- Next.js with Supabase auth (email + password). Sign up,
sign in, sign out, protected routes that redirect to sign-in
when the user isn't authenticated.
- Supabase database with these tables (give me the SQL, I'll
run it in the SQL Editor):
- profiles (id references auth.users, wallet_address text,
display_name text)
- posts (id, creator_id references profiles, title,
cover_path, body, price_mon numeric, created_at)
- subscriptions (id, subscriber_id references profiles,
creator_id references profiles, tx_hash text unique,
paid_amount_mon numeric, created_at)
- Enable RLS on every table. For now, give me permissive
policies I'll tighten later.
- Read SUPABASE_URL and SUPABASE_KEY directly from process.env.
Do NOT create a .env file. Do NOT rename the variables.
- Minimal UI. Plain system fonts. We'll style later.
Run it locally so I can test sign-up and sign-in.
Take the SQL the agent hands back, paste it into Supabase's SQL Editor, click Run, and confirm sign-in works before moving on.
Creators Post Content
Add the creator flow: write a post, attach a cover image, set a price.
> Add a "write post" page that only signed-in users can reach.
It has a title input, a body textarea, a JPG-only file input
for the cover image, and a number input for price in MON. On
submit: upload the image to a "post-covers" Supabase storage
bucket under path "{creator_id}/{timestamp}.jpg", insert a row
into posts with the cover_path, and redirect to the post.
Validate: body non-empty, title under 140 chars, price >= 0.
Tighten RLS on Posts
The permissive policies from the scaffold let anyone write to the posts table. Fix that:
> Rewrite the RLS policies on posts:
- SELECT: anyone can read a row's id, title, cover_path, and
created_at. The body column is only readable if the user is
the creator OR has a matching row in subscriptions for that
creator_id. Implement this by exposing only the safe columns
via a Postgres VIEW for unauthenticated reads, and gating
the body column through an RLS policy on the posts table.
- INSERT: only rows where creator_id = auth.uid().
- UPDATE / DELETE: only rows where creator_id = auth.uid().
Give me the SQL so I can paste it into the SQL Editor.
Security. The paywall is the RLS policy. The body column itself is the thing behind the wall. If the SELECT policy is wrong, the body leaks. Test it: sign up as a non-subscriber and verify the body is
nullin the response. Do not enforce the paywall in your frontend code. Frontend code is suggestion, not security.
Where should the paywall actually be enforced?
Readers Pay With a Wallet
The subscription is an on-chain payment. The reader connects their wallet, pays the creator's price, and the transaction hash becomes the proof of subscription.
> On a creator's profile page, show a "Subscribe" button when
the reader is signed in and not already subscribed to that
creator. Clicking it:
- Opens the reader's connected wallet (use wagmi + viem) and
requests a transfer of price_mon MON to the creator's
wallet_address from the profiles table.
- Waits for the tx to confirm on Monad testnet (or mainnet
based on NEXT_PUBLIC_CHAIN).
- POSTs to /api/subscriptions with { creator_id, tx_hash,
paid_amount_mon }.
- The /api/subscriptions route uses viem on the server to
fetch the tx from the chain and verifies:
* tx.to === creator's wallet_address
* tx.value >= price_mon (in wei)
* tx.from === reader's wallet_address (auth check)
* tx_hash is unique in subscriptions (prevents replay)
Only then does it insert the subscription row.
Show a clear error if any check fails.
Security. Verify server-side, always. Never trust the client's claim that a payment happened. The route must fetch the transaction from the chain and check
to,value, andfrom. Thetx_hash uniqueconstraint prevents the same transaction from being submitted twice. Without these checks, a reader can POST any hash and get a free subscription.
Why does the /api/subscriptions route re-fetch the transaction from the chain?
Email on New Posts
When a creator publishes, every subscriber gets an email with the post title and a link.
> When a creator inserts a new row into posts, email every
subscriber of that creator. Use the email provider we set up
in the email lesson (Resend). Queue the sends from a Supabase
Edge Function (or a background job). Do not block the
create-post request on email delivery, the post should save
and render immediately whether the emails succeed or not.
Template: creator display_name, post title, cover image, a
short preview of the body, and a link. Sanitize all user
content before rendering into the HTML template.
Security. Rate-limit and sanitize. A creator with many subscribers triggers many emails. Cap sends per post, log every send, and never let a post author inject raw HTML into the email body. Sanitize with a known-good library before rendering.
Why queue emails in a background job instead of sending them during the create-post request?
Performance: Index the Feed
A popular creator will have thousands of posts and tens of thousands of subscribers. Two queries matter:
- "Show me all posts by this creator, newest first": the creator's feed.
- "Is this reader subscribed to this creator?": the paywall check, run on every paywalled read.
Both should hit an index.
CREATE INDEX posts_creator_created_idx
ON public.posts (creator_id, created_at DESC);
CREATE INDEX subscriptions_subscriber_creator_idx
ON public.subscriptions (subscriber_id, creator_id);
Paste into Supabase's SQL Editor and run.
Also paginate the feed:
> Change the creator page to render 20 posts at a time with a
"Load more" button that fetches the next 20. Query with
.range(offset, offset + 19) using the index above. Return
only the public columns (id, title, cover_path, created_at)
for the feed. Don't fetch the body until the reader opens
the post.
Why include both columns in the subscriptions index?
Try It End-to-End
Sign up as User A, set a wallet address, write a post, set a price.
Sign up as User B in a different browser (or incognito), connect a wallet with some test MON, open User A's profile, and subscribe. The transaction confirms on-chain. The subscription row lands in the database. User B can now see the full body.
Sign up as User C without subscribing. Confirm User A's post body is empty or hidden. If C can see the body, your RLS policy is wrong, go fix it before continuing.
Have User A write a new post. User B receives an email within a minute.
That's the whole flow.
Security Recap
Run through the checklist before calling this done:
- RLS on every table. The paywall lives here.
- Keys: Supabase anon key in env vars;
service_rolekey never in client code. - Payments: verified server-side by re-fetching the tx from the chain. Tx hash has a unique constraint.
- Uploads: MIME and size caps, private bucket unless the content is truly public.
- Email: sanitize user input before rendering into templates; rate-limit sends per post.
- Indexes: queries on large tables hit an index, not a full scan.
- Auth: every API route re-checks the user's identity server-side, it does not trust the client.
What You Just Built
An end-to-end product that uses everything you've learned in the sophomore track:
- Database and storage hold the posts, subscriptions, and cover images.
- Auth and RLS keep the paywall honest.
- Crypto wallet with on-chain payment records subscriptions on Monad.
- Email notifications keep subscribers in the loop.
- Performance keeps the app snappy as content and readers grow.
- Security is the thread running through every step.
This is a legitimate product idea, not a toy. Paid newsletters are a real market. If you like this one, keep building on it.
What's Next
You've gone from scaffolding a single-page app to shipping a product that moves real money, stores real data, and protects real users. Next up: Build an On-Chain Space Shooter — a different kind of app, where the thing that needs protecting isn't a subscription, it's a leaderboard nobody can fake.
0/6 correct
0% — get all correct to complete