Mini lesson · English

How to keep a page private

Use a ready sign-in project to keep a practice note private and test both allowed and denied access.

Start the interactive lessonRead the lesson below

Who is this for?

People who want a page or note available only after sign-in. A coding assistant can connect the supplied protection; server programming is not the learning requirement.

Before you start

The supplied sign-in setup or equivalent Supabase access, a separate function-hosting project and a coding assistant. Use a fictional PRIVATE_NOTE kept only in the server environment, never real private content.

Estimated time: 20–32 min

Estimates for the supplied starter files with prerequisite accounts and tools ready. Setup, deployment queues and delivery/DNS waits take additional time. This example allows all valid users of the practice project. Per-user records and roles require additional authorization rules.

What you will learn

  • Keep private content out of the public page.
  • Test the same route signed out and signed in.
  • Check an access failure without revealing the note.

Workshop files

Step 1

Choose what needs protection

Private means unavailable to an unauthorized visitor, not merely hidden.

Use the supplied fictional note and ready project. Ask the assistant to keep the note on the server rather than inside the downloaded public page.

A visitor can inspect public files even when an element is hidden. Do not put real private information into the exercise.

A concrete example

A paragraph concealed by page styling is still part of the public file.

Use this complete private-note workshop and my managed sign-in practice project. Keep PRIVATE_NOTE only in the private server environment and preserve the supplied access checks. Help run the app, then compare signed-out direct access with verified sign-in. Do not treat hiding a paragraph as protection or expose another person’s data.

Files for this step

Start hereWorkshop files

Open or download the files below. When saving a project, keep the folder paths shown in the filenames.

How it works (optional)

Protect the response, not just the button

Private content must not be shipped to an unauthorized browser in the first place.

CSS hiding and disabled buttons change the interface, not the response. Anyone can inspect public files and issue a direct request to an endpoint.

Our server reads a note from its environment and returns it only after verifying a user token. The fictional note is for the lab; real private data belongs in a protected data source, not a public repository.

Worked example

A public JavaScript file containing a “hidden” note is still a public copy. The example instead keeps the value out of public/.

Extra implementation exercise

The page hides a paragraph until sign-in, but the paragraph is in the HTML source. Is it private?

Show a worked solution

No. Gate the server response before sending the content. Hiding a received value cannot make it secret.

  • Recognizes that the browser already received it.
  • Moves access control before the protected response.

Step 2

Connect the ready protection

Let the assistant configure the existing server checks.

Follow run-locally.txt with your assistant. Before testing, confirm the intended authentication project, its public browser values, the private server authentication settings and the fictional PRIVATE_NOTE. Start the complete project and open the actual proxy URL; a plain file preview cannot run its protection.

These account/server readiness checks are required. Reading the function implementation is optional. The service must verify the account session before returning the note; a caller typing another account ID into a request is not proof of identity.

A concrete example

A user-supplied identity is not the same as a confirmed account session.

Supabase: verified user retrieval · Node.js: supported release status

Files for this step

Start hereWorkshop files

Open or download the files below. When saving a project, keep the folder paths shown in the filenames.

How it works (optional)

Send a session token to the server

The client supplies a bearer token; the server verifies it independently.

Configure the sign-in files and server environment in START-HERE.txt. The client obtains its current session and sends its access token in the Authorization header when requesting the private endpoint.

The server calls the provider’s user endpoint with that token and the project’s publishable application key. It rejects missing or invalid tokens, and distinguishes a provider outage from accepted identity. Decoding a token without verifying it is not sufficient.

Follow the request in order: browser sends its user access token → the server asks the identity provider whether that token represents a user → only a verified result permits returning the note. The project publishable key identifies the application; it does not by itself identify or authorize the person.

For the complete local sequence, open run-locally.txt: prepare the private root environment, link only the practice project and start the Netlify function proxy. server-env.example.txt lists variable names without real secrets. Opening only the HTML cannot run the server.

Worked example

A caller-supplied userId field is not proof that the caller owns that ID. Use the identity returned by verification.

const r=await fetch('/.netlify/functions/mini-private-note', {
 headers:{Authorization:'Bearer '+session.access_token}
});

Supabase: verified user retrieval · Node.js: supported release status

Extra implementation exercise

Could the server authorize a request by trusting userId from the browser JSON?

Show a worked solution

No. The browser can change that field. Verify the credential and use the verified identity.

  • Treats client identity claims as untrusted.
  • Uses a verified token and resulting identity.

Step 3

Test the allowed audience

Decide who should be able to read this particular item.

Sign in with your controlled test account and request the fictional note. For this lab the rule is any verified signed-in user; ask the assistant to preserve that stated scope.

If you later store one person’s account data, that broader rule is not enough. State the audience rather than assuming all private data has identical permissions.

A concrete example

A shared member note and an individual account record need different access decisions.

Files for this step

Start hereWorkshop files

Open or download the files below. When saving a project, keep the folder paths shown in the filenames.

How it works (optional)

Return only the intended protected data

A valid identity does not automatically grant access to every user’s records.

This small example grants every valid user in the practice project access to one shared note. That is an explicit authorization rule, not a universal definition of private content.

The response uses private, no-store caching so a shared cache should not retain the note. A later profile lesson uses ownership rules for per-user records. Do not expand this handler into a global database dump.

Worked example

The server’s viewerId is derived from verification. The note comes from its environment, not from data supplied in the request.

Extra implementation exercise

If the note were replaced with another user’s account details, would “any signed-in user” remain a suitable rule?

Show a worked solution

No. Add a rule matching the authenticated identity to the requested record or role. A login alone does not grant access to another account.

  • Distinguishes authentication from record-level authorization.
  • Requires ownership or an appropriate role for per-user data.

Step 4

Try the same route signed out

A direct request matters as much as the normal button.

Use a genuinely signed-out browser profile, or sign out and verify the account state, then ask your assistant to request the same note directly. An extra private window may share an existing private session. Repeat the note request with verified sign-in.

The signed-out result must not contain the note; the allowed session should receive it. A request failure should stay an error without leaking content.

A concrete example

Removing a menu link does not protect the address it used to open.

Files for this step

Start hereWorkshop files

Open or download the files below. When saving a project, keep the folder paths shown in the filenames.

How it works (optional)

Try the endpoint without the interface

A real access test targets the data request directly.

Request the function URL in a fresh window without an Authorization header; it should not return the note. Try a malformed bearer token, then use the signed-in UI to make an authorized request.

Sign out and check that the app clears the displayed note and does not send a session token again. A previously issued token may remain valid until its configured expiry; immediate global revocation is a separate requirement.

Worked example

A denied direct request demonstrates more than a screenshot of a login button. Keep the response status and whether protected text was present in your record.

Extra implementation exercise

A direct request returns 401 with no note, while a verified session receives the note. What boundary did you test?

Show a worked solution

You tested that this endpoint verifies identity before returning the shared note. Other endpoints and ownership rules need their own tests.

  • Identifies the server response boundary.
  • Does not claim universal token revocation or per-user authorization beyond the example.

Step 5

Protect one more test value

Repeat the same checks after a small change.

Ask the assistant to add a second fictional server-only value using the same intended audience rule. Do not move either value into public files for convenience.

Repeat allowed, denied and failure observations. Keep the handoff redacted and label any test not run.

A concrete example

Adding another field should not accidentally make the original private note public.

Files for this step

Start hereWorkshop files

Open or download the files below. When saving a project, keep the folder paths shown in the filenames.

How it works (optional)

Your assignment: add a second protected value

Keep the new value out of public files and apply the same server checks.

Add another practice value in the server environment, return it only after verification and display it safely as text. Keep the response non-cacheable.

Repeat missing-token, invalid-token and valid-session tests. Search the public source to confirm that neither private value was embedded there.

Worked example

The assignment extends the protected response, not the amount of data shipped before sign-in.

The complete reference walkthrough is available in reference/assignment-walkthrough.txt. Compare its steps with your own result; fill the test record from observations in your own project.

Extra implementation exercise

Protect a second value and provide both positive and negative access evidence.

Show a worked solution

Call the private-note endpoint directly with no Authorization header: expect 401 and no note. A made-up bearer token must also fail. The browser's userId or a hidden button does not satisfy the server's identity check.

With your actual verified session token in your own practice project, expect the configured server note and Cache-Control: private, no-store. Keep the note out of public HTML/JavaScript. Do not paste a real bearer token into a shared test report.

After sign-out, the UI should clear private content and stop making authenticated requests. An already copied access token may remain usable until expiry: sign-out UI behaviour is not an instant-revocation guarantee. This shared-note lab authorizes signed-in users; per-user ownership is the separate profile exercise.

  • Both values remain server-side until authorization succeeds.
  • Unauthorized direct requests contain neither value; valid access and no-store behavior are checked.

Was this lesson useful?

Leave a like, or share it with someone who could use it.

Continue learning

Explore the other lessons