LessonsHow to keep a page private

Mini lesson 12 of 30 · Accounts and access

Choose what needs protection

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.

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

Purpose of this lesson

  • 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.
Explanation, worked example and practice

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.

Check your starting skills

These are useful skills, not locked course requirements. If you can already do them, start this lesson; otherwise open the linked preparation. The supplied starter files mean you do not need an existing personal website.

  1. How to let people sign in to your website

    Request and use a real sign-in code.

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.

Understand the idea

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.

Try it yourself

What note are you testing, and how will you check that signed-out visitors cannot obtain it?

Show a worked solution

Illustrative plan: “My fictional note is a practice meeting reminder. Any verified signed-in test user may read it in this lab. I will try the note route in a separate signed-out window and confirm that its response contains no note.”

The note must stay outside public page files. Concealing a paragraph visually would not prevent a visitor from reading its downloaded source.

What a sound answer includes

  • Uses a fictional server-only note and states the lab’s signed-in-user audience.
  • Plans a direct signed-out request, rather than relying on a hidden page element.
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 1 · How to keep a page private

Connect the ready protection

Let the assistant configure the existing server checks.

Explanation, worked example and practice

Understand the idea

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.

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.

Try it yourself

Which setup step is complete, and what actual app URL can you test?

Show a worked solution

Illustrative readiness record: “The supplied app is running at the local proxy URL printed by the server. The practice authentication project and fictional server note are configured. The note is absent from the public page files.”

A blocked record could read: “The page opens, but the server reports Auth not configured; the assistant must check the private provider environment before I test access.” The page opening by itself does not establish server protection.

What a sound answer includes

  • Identifies the running application address and completed server/provider configuration.
  • Reports a concrete setup blocker without publishing the private note or credentials.
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 2 · How to keep a page private

Test the allowed audience

Decide who should be able to read this particular item.

Explanation, worked example and practice

Understand the idea

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.

Try it yourself

Who is allowed to read your practice note, and did the signed-in request match that rule?

Show a worked solution

Illustrative access record: “Test account A is verified and received the fictional member note. The lab rule is any verified signed-in user, so another verified test member is also within the allowed audience.”

This is a shared-member rule. It would be insufficient for A’s personal profile, which must be restricted to A; that different boundary needs an ownership check.

What a sound answer includes

  • States that the lab permits any verified signed-in user, not only one note owner.
  • Records the allowed account’s result without extending this rule to personal account data.
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 3 · How to keep a page private

Try the same route signed out

A direct request matters as much as the normal button.

Explanation, worked example and practice

Understand the idea

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.

Try it yourself

What did the direct signed-out and signed-in attempts each return?

Show a worked solution

Illustrative direct-request record: “Signed out: the note route returned Sign in required and no note. Signed in as A: the same route returned the fictional note. The assistant helped inspect those response bodies without sharing tokens.”

If the signed-out response includes the note, access protection failed even if the page hides it. If the authentication service is unavailable, record that failure separately from an ordinary denied request.

What a sound answer includes

  • Compares the same note route with a signed-out request and an allowed signed-in request.
  • Checks that denied or failed responses contain no private note text.
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 4 · How to keep a page private

Protect one more test value

Repeat the same checks after a small change.

Explanation, worked example and practice

Understand the idea

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.

Try it yourself

Provide the new value’s protection and the observed access results.

What to produce: The working practice route and observed signed-out/signed-in/error access checks.

Show a worked solution

Illustrative extension record: “I added a fictional meeting location beside the original member note. A verified test member received both; the signed-out direct request received neither. With authentication deliberately unavailable in the disposable setup, the response stayed an error and contained neither value.”

A handoff can identify the two fields and these outcomes without reproducing their contents. If a failure case was not run, list it as not run rather than treating a successful signed-in check as proof of every boundary.

What a sound answer includes

  • Keeps both fictional values server-only under the stated shared-member rule.
  • Retests allowed, denied and service-failure responses for the added value.
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.