Courses / Mobile Application Development (CDE-315) – BS CS 5th Semester
Project Step 7 – Supabase Backend for Your App: Tables, Row Level Security and Email Sign-in
Course Project (Java) – Build a Modern Cloud App Step by Step (CampusPulse + 7 ideas) · 90 min read · 4 MCQs to check yourself
Supabase – Advanced Row Level Security policies
Goal
Create the cloud data tier for your app on Supabase: two tables (events and feedback), Row Level Security so users can only change their own data, and email sign-in. Do the Supabase Week theory and lab first – this step uses the same skills.
7.1 Create the project
- supabase.com → New project → name
campuspulse, strong database password, region near you → Create. - Copy the Project URL and the publishable key (Project Settings → API Keys).
- Put them in your Android project's
local.properties(it is not uploaded to Git):
SUPABASE_URL=https://YOUR-PROJECT.supabase.co
SUPABASE_KEY=sb_publishable_YOUR_KEY
Then File → Sync Project with Gradle Files.
7.2 Tables and rules – run in the SQL Editor
-- ============ EVENTS: the main record of the app ============
create table public.events (
id uuid primary key default gen_random_uuid(),
created_at timestamptz not null default now(),
user_id uuid not null default auth.uid() references auth.users (id) on delete cascade,
title text not null check (char_length(title) between 3 and 120),
description text check (char_length(description) <= 2000),
category text not null default 'General',
starts_at timestamptz not null,
venue text,
lat double precision check (lat between -90 and 90),
lng double precision check (lng between -180 and 180),
contact_phone text,
contact_email text,
link_url text
);
create index events_starts_at_idx on public.events (starts_at);
alter table public.events enable row level security;
create policy "signed-in users read events" on public.events
for select to authenticated using (true);
create policy "users add their own events" on public.events
for insert to authenticated with check (auth.uid() = user_id);
create policy "users edit their own events" on public.events
for update to authenticated using (auth.uid() = user_id) with check (auth.uid() = user_id);
create policy "users delete their own events" on public.events
for delete to authenticated using (auth.uid() = user_id);
-- ============ FEEDBACK: from the Feedback tab (Lab 17) ============
create table public.feedback (
id uuid primary key default gen_random_uuid(),
created_at timestamptz not null default now(),
user_id uuid not null default auth.uid() references auth.users (id) on delete cascade,
rating int not null check (rating between 1 and 5),
category text not null default 'Suggestion',
message text not null check (char_length(message) between 10 and 2000),
lat double precision,
lng double precision
);
alter table public.feedback enable row level security;
create policy "users send feedback" on public.feedback
for insert to authenticated with check (auth.uid() = user_id);
create policy "users see their own feedback" on public.feedback
for select to authenticated using (auth.uid() = user_id);
What the rules mean
| Table | Read | Add | Change / delete |
|---|---|---|---|
| events | every signed-in user | only as yourself | only your own events |
| feedback | only your own | only as yourself | nobody (admins read it in the dashboard) |
user_id … default auth.uid() fills the owner automatically from the token – the app never sends it, so nobody can post as someone else.
7.3 Sign-in settings
Authentication → Sign In / Providers → Email:
- Enable Email provider: ON.
- Confirm email: switch OFF for the class project (testers can sign in immediately). If you leave it ON, the app still works: after registering, the user confirms from their inbox, then signs in.
- Minimum password length: 6 (default) – our form checks this too.
7.4 Add sample events (Table Editor)
Sign up once from the app after Step 8, or create a user now in Authentication → Users → Add user. Then in the SQL Editor (replace the email):
insert into public.events (user_id, title, description, category, starts_at, venue, lat, lng, contact_phone, link_url)
select id, 'Tech Talk: AI on Phones', 'How on-device AI works – with live demos.', 'Tech',
now() + interval '3 days', 'Seminar Hall, Block C', 33.6844, 73.0479, '0511234567', 'https://www.example.edu.pk'
from auth.users where email = 'you@example.com';
7.5 Test with curl before touching the app
Sign in and get a token:
curl -X POST "https://YOUR-PROJECT.supabase.co/auth/v1/token?grant_type=password" \
-H "apikey: sb_publishable_YOUR_KEY" -H "Content-Type: application/json" \
-d '{"email":"you@example.com","password":"your-password"}'
Copy access_token from the reply, then read events:
curl "https://YOUR-PROJECT.supabase.co/rest/v1/events?select=*&order=starts_at.asc" \
-H "apikey: sb_publishable_YOUR_KEY" -H "Authorization: Bearer PASTE_ACCESS_TOKEN"
Without the Authorization header you get [] – the select policy is only for authenticated users. That is RLS working.
Check before moving on
- Both tables exist with RLS enabled (a shield icon / "RLS enabled" in the Table Editor).
- The curl sign-in returns an access token; reading events with the token returns your sample row.
-
local.propertieshas the URL and publishable key; no secret key anywhere.
Adapt it to your project
Rename the table and columns for your idea – but then use the same names in @SerializedName (Event.java) and in the URLs (SupabaseApi.java). Examples:
- BloodLink:
requests(blood_group, units, needed_by, hospital, lat, lng, contact_phone). - ThriftHub: add
price numeric(10,2) check (price >= 0)andis_sold boolean default false. - CleanCity: add
status text default 'Open' check (status in ('Open','In progress','Fixed')). - Want everyone (even signed-out users) to read? Change the select policy to
to anon, authenticated.
Short and long questions
1. Project viva: Write the RLS policies of the events table and explain them.
Model answer
select to authenticated using (true) – any signed-in user reads events; insert to authenticated with check (auth.uid() = user_id) – you can only add rows as yourself; update and delete to authenticated using (auth.uid() = user_id) – only the owner can change or delete. user_id defaults to auth.uid().
2. Design the Supabase data tier for your own project idea (not CampusPulse): tables, columns with types and constraints, and RLS policies. Justify each policy.
Model answer
Answer depends on the idea. Example BloodLink: requests(id uuid pk default gen_random_uuid(), created_at timestamptz default now(), user_id uuid default auth.uid() references auth.users, blood_group text check in (A+,A-,B+,B-,O+,O-,AB+,AB-), units int check (units between 1 and 10), needed_by timestamptz not null, hospital text not null, lat/lng double precision, contact_phone text not null); RLS: select to authenticated using (true) so donors see all requests; insert with check (auth.uid() = user_id); update/delete using (auth.uid() = user_id) so only the requester closes it. Marks for correct types, constraints, defaults, and policies that protect ownership.
This lesson ends with a quiz of 4 MCQs. Open the lesson on PrepOasis to take it and save your progress.
Previous: Project Step 6 – Profile, Feedback with Undo Countdown, and HTML5 About Page (Labs 2, 3, 6, 11, 13, 15, 17) · Next: Project Step 8 – Connect the App to Supabase with Retrofit (Lab 12 + Supabase Week)
All lessons in Mobile Application Development (CDE-315) – BS CS 5th Semester