LessonsHow to let users upload images

Mini lesson 14 of 30 · Accounts and access

Understand what the preview proves

Who is this for?

People who want visitors to upload images. You can use the ready project and a coding assistant without writing storage policies or upload code.

A selected filename, a stored upload and a readable image are three different observations.

Purpose of this lesson

  • Distinguish a local preview from a stored upload.
  • Test allowed and rejected supplied files.
  • Check owner-only deletion and another account’s limits.
Explanation, worked example and practice

Before you start

A separate Supabase practice project with two controlled test accounts. The small, oversized and unsupported files are supplied. Ask the assistant to prepare the private avatars storage and provided policies in this disposable project.

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: 25–42 min

Estimates for the supplied starter files with prerequisite accounts and tools ready. Setup, deployment queues and delivery/DNS waits take additional time. This lab covers restricted image storage and ownership. Client checks and MIME settings do not constitute a complete malware/content-validation pipeline for arbitrary public uploads.

Understand the idea

Open the supplied app with your assistant and choose fixtures/avatar-small.webp. Before pressing Upload image, the file control shows your selected filename. This starter does not create a local image preview at selection time; its image appears only after upload and signed access succeed.

Other apps can preview a local image without storing it. In this app too, selecting a filename alone proves no remote upload. Keep selection, provider storage and the later image read as separate checks.

A concrete example

Selecting avatar-small.webp can show the filename before the storage account is configured. To establish remote storage later, find the uploaded object in the provider and check the signed preview that reads it.

Use the complete image-upload workshop and my disposable Supabase project. Help configure sign-in, a private avatars bucket, the stated 2 MB limit and allowed image formats with the provided policies. Keep private keys out of public files. Run the page and guide me through small/oversized/unsupported-file tests and separate-account deletion checks. Handle code changes for me.

Files for this step

fixtures/avatar-small.webp
fixtures/avatar-small.webpDownload this file

Start hereWorkshop files

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

Try it yourself

Select the supplied small image without uploading it. What does the file control show, and what storage/read evidence is still missing?

Show a worked solution

Illustrative pre-upload observation: “Selecting avatar-small.webp showed its filename in the file control. This starter did not yet show an uploaded image. I have not pressed Upload image or found a stored object.”

I still need to upload, locate the object in the provider and check its short-lived preview. Other apps may show a local image before uploading; such a preview would not prove remote storage either.

What a sound answer includes

  • Reports the selected filename separately from an image returned after upload.
  • Names the provider object and later successful read as evidence still needed.
How it works (optional)

Store the image, not just a local preview

A preview created from a selected file is not evidence that a storage service received it.

The browser first holds the selected file. Uploading sends its bytes to an object-storage service, which gives it a path and applies access rules.

We will use a private Supabase bucket with one owner folder per account. The app uploads a file, then asks for a short-lived signed preview URL. It does not make every upload publicly readable.

Worked example

A local blob: preview can work without any network request. Our test will inspect the actual object and retrieve it through an authorized route.

Extra implementation exercise

The selected image appears instantly through a blob URL. Has the upload necessarily happened?

Show a worked solution

No. Verify the storage operation and matching object before claiming an upload.

  • Separates local preview from remote storage.
  • Checks the upload result and stored object.

Step 1 · How to let users upload images

Prepare private test storage

Use the supplied settings and an assistant for the setup.

Explanation, worked example and practice

Understand the idea

In your disposable project, have the assistant configure the private avatars bucket, size/format limits and policies.sql. Launch the complete sign-in/upload app over HTTP.

Confirm the bucket is private and the account/project is the intended one. Do not remove storage protections to make a failed request succeed.

A concrete example

The visitor interface can help reject a file, but the storage service must enforce its own rules too.

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 storage settings were applied, and can your test account open the running app?

Show a worked solution

Illustrative setup record: “The assistant prepared the private avatars bucket in the disposable project, set the stated 2 MB PNG/JPEG/WebP limits and applied the supplied owner-folder policies. The HTTP app opens and A can sign in.”

If upload fails before those checks are complete, record which setup stage is missing. Making the bucket public or removing ownership rules is not a valid shortcut.

What a sound answer includes

  • Identifies the disposable project, private avatars bucket and configured format/size limits.
  • Records the running sign-in/upload app and any setup blocker without weakening storage protections.
How it works (optional)

Create a private bucket and owner rules

Bucket configuration and access policies enforce different parts of the task.

Create the avatars bucket as private. Configure its 2MB size limit and allowed MIME types, then apply policies.sql. Browser roles should only insert and read paths under their verified user ID.

The client’s accept attribute is a convenience, not a security check. Provider size/type rules and ownership policies still apply to direct requests. MIME labels alone do not prove arbitrary file content is safe; do not present this lab as a complete scanning service.

For an object named USER_A/avatar-1.webp, storage.foldername(name) returns the folder components; [1] selects the first one in this PostgreSQL array. It must equal the authenticated user's ID. USER_B/avatar-1.webp fails for account A. This storage-side rule is distinct from the browser's file picker and MIME precheck.

The first path segment remains the user ID even with nested folders: USER_A/subfolder/avatar.webp still has USER_A as folder component [1]. A path beginning subfolder/USER_A/... would not match. Keep the owner ID first; do not loosen the policy just to accommodate a mistaken path.

Worked example

The folder prefix comes from the signed-in user, while the database policy compares it with auth.uid(). Changing a browser path to another user’s folder should not grant permission.

create policy "upload own avatar" on storage.objects for insert to authenticated
with check (bucket_id='avatars' and (storage.foldername(name))[1]=auth.uid()::text);
create policy "read own avatar" on storage.objects for select to authenticated
using (bucket_id='avatars' and (storage.foldername(name))[1]=auth.uid()::text);

Supabase: storage access control

Extra implementation exercise

Why is checking the filename extension only in JavaScript insufficient?

Show a worked solution

A caller can send a direct request with different metadata or a different path. Enforce the intended bucket and owner rules at the service boundary.

  • Recognizes the caller can bypass JavaScript.
  • Requires storage-side restrictions and authorization.

Step 2 · How to let users upload images

Upload the small test image

Check the stored object as well as the visible picture.

Explanation, worked example and practice

Understand the idea

Sign in as account A and upload the supplied small image. Find its object in the provider and use the resulting preview or signed access route.

If storage succeeds but preview fails, investigate reading/preview before uploading another copy. Keep the recorded object reference.

A concrete example

A missing preview does not necessarily mean the upload itself failed.

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

Was the small image stored, and could it be read afterward?

Show a worked solution

Illustrative upload record: “A uploaded avatar-small.webp. The provider showed an object under A’s folder; I saved its redacted reference. The image then appeared through the short-lived signed preview.”

A partial result could read: “The object exists, but preview access failed.” That calls for a read/access check on the same object, rather than uploading repeated copies without knowing whether storage succeeded.

What a sound answer includes

  • Links A’s successful upload to the actual stored object reference.
  • Separately records whether the signed preview could read that object.
How it works (optional)

Upload to a unique owner path

Use a generated name instead of trusting a caller’s path or overwriting an existing file.

After sign-in, the app chooses a path using the verified user ID and a random filename. It checks the selected image’s size/type before requesting an upload, but those checks do not replace provider enforcement.

The upload does not enable upsert. If storage succeeds but signed-URL creation fails, the UI reports those as separate observations. It never says “nothing uploaded” when the object may already exist.

Worked example

A generated filename avoids accidental collisions with another picture named photo.jpg. It is not an authorization mechanism by itself.

Extra implementation exercise

The object exists, but preview creation failed. Should you immediately upload another copy?

Show a worked solution

Check the object path and read policy first. Re-uploading can create duplicates without repairing the preview problem.

  • Keeps upload success separate from preview authorization.
  • Investigates read policy/path before creating duplicates.

Step 3 · How to let users upload images

Try the supplied failure cases

The exercise includes files that should not upload.

Explanation, worked example and practice

Understand the idea

Use fixtures/avatar-too-large.png and fixtures/not-an-image.txt. Record how the page and storage respond. Ask the assistant to inspect a rejection that is confusing or an invalid file that slipped through.

Rejected files must not become stored objects. A valid signed URL can allow its holder to view an image for its lifetime; that alone is not proof that the bucket became public.

A concrete example

A file-picker restriction and real storage enforcement are different checks.

Files for this step

fixtures/avatar-small.webp
fixtures/avatar-small.webpDownload this file

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 happened for the oversized and unsupported files, and what access route did you actually test?

Show a worked solution

Illustrative rejection record: “The oversized PNG was rejected and no new object appeared. The text file was rejected too; the assistant helped test it if the file picker hid unsupported formats.”

“The allowed image’s signed URL opened during its short lifetime. That showed access through that URL, not that the private bucket became public.” A file-picker filter alone is not a direct test of the provider’s storage limits.

What a sound answer includes

  • Records separate rejection and storage observations for both supplied invalid files.
  • Distinguishes a temporary signed-link read from unrestricted public-bucket access.
How it works (optional)

Verify rejection and temporary access

Test the service rules and understand what a signed link grants.

Upload fixtures/avatar-small.webp as account A. Inspect its storage path and open the authorized signed preview. Then try fixtures/avatar-too-large.png (over 2MiB) and fixtures/not-an-image.txt (unsupported type); both should be rejected. All three files are supplied with the lesson.

Use account B to request a signed link for A’s path; the owner policy should deny it. A signed URL legitimately issued to A can still be used by anyone who obtains it until expiry. Client sign-out does not instantly revoke every issued URL.

Worked example

A useful report records a redacted path and expiry duration, not the full temporary access URL.

Extra implementation exercise

Someone with a valid signed URL opens the image without signing in. Does that alone prove the private bucket policy failed?

Show a worked solution

No. Test whether an unauthorized account can create the URL or read without an authorized capability. Treat issued URLs as sensitive until they expire.

  • Recognizes signed URLs as temporary bearer capabilities.
  • Distinguishes URL access from permission to create one.

Step 4 · How to let users upload images

Add owner-only deletion

Verify who can remove the uploaded object.

Explanation, worked example and practice

Understand the idea

Ask the assistant to add the supplied owner-deletion behavior. As A, delete a first test object X and verify it is absent in storage. Then upload a new object Y as A; keep Y’s redacted reference for the next check.

In B’s separate session, have the assistant target Y for a direct deletion attempt. Record actor B, target Y and the provider result, then verify that A’s Y still exists. Checking only a hidden delete button, or trying to delete already-missing X again, would not establish this ownership boundary.

A concrete example

Removing an image from the screen is not the same as deleting the stored object.

Files for this step

fixtures/avatar-small.webp
fixtures/avatar-small.webpDownload this file

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 owner deletion and the second-account attempt with actual object results.

What to produce: The upload/delete result and actual valid, oversized, unsupported and cross-account checks.

Show a worked solution

Illustrative deletion record: “A deleted object X and the provider confirmed X was absent. A then uploaded fresh object Y. B’s separate session attempted to delete Y; A checked that Y still existed.”

The assistant should identify actor B and target Y before running the isolated test. Attempting to delete X again would test an already-missing object, not owner-only protection.

What a sound answer includes

  • Deletes A’s first object as its owner and verifies that exact object is gone.
  • Uploads a fresh A-owned object, targets it from B and verifies that it remains stored.
How it works (optional)

Your assignment: add owner-only deletion

A new operation needs its own policy and negative tests.

Add a delete control for the uploaded object and a DELETE policy restricted to its owner folder. Do not broaden the existing bucket into a public shared area.

Verify that A can remove A’s object and B cannot remove it. Keep the storage state consistent with the UI after a failed deletion.

Worked example

The original policies intentionally allow insert/read only. Deletion is not automatically authorized by an upload permission.

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

Implement owner-only deletion and demonstrate both allowed and forbidden attempts.

Show a worked solution

Use a private avatars bucket with storage-side size/type settings and owner-folder policies. Upload an allowed image as account A; inspect the returned storage result, then open the short-lived signed preview. A blob preview alone proves no remote upload.

Try an unsupported or oversized file and attempt an upload/read under account B's folder. A must not gain access merely by changing a path in developer tools. Signed URLs intentionally grant temporary access until their expiry.

For deletion, add a DELETE policy using the same bucket/owner-folder condition and call remove with the exact stored path. Verify the object is absent afterward and a cross-owner deletion leaves the other owner's object intact. The reference deletion module is separate so you can inspect the path and policy before integrating it.

  • Delete permission checks the bucket and verified owner folder.
  • Failed attempts do not remove another account’s object or falsely clear the UI.

Was this lesson useful?

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