Click here to see an example `psql` interaction using this.
+ +This example shows that the `public.profiles` table from the tutorial example can indeed be updated by the `postgres` role and the owner of the row but not from `anon` connections: + +```shell +postgres=> select id, email from auth.users; + id | email +--------------------------------------+------------------- + d4f0aa86-e6f6-41d1-bd32-391f077cf1b9 | user1@example.com + 15d6811a-16ee-4fa2-9b18-b63085688be4 | user2@example.com + 4e1010bb-eb37-4a4d-a05a-b0ee315c9d56 | user3@example.com +(3 rows) + +postgres=> table public.profiles; + id | updated_at | username | full_name | avatar_url | website +--------------------------------------+------------+----------+-----------+------------+--------- + d4f0aa86-e6f6-41d1-bd32-391f077cf1b9 | | user1 | User 1 | | + 15d6811a-16ee-4fa2-9b18-b63085688be4 | | user2 | User 2 | | + 4e1010bb-eb37-4a4d-a05a-b0ee315c9d56 | | user3 | User 3 | | +(3 rows) + +postgres=> call auth.login_as_anon(); +CALL +postgres=> update public.profiles set updated_at=now(); +UPDATE 0 -- anon users cannot update any profile but see all of them +postgres=> table public.profiles; + id | updated_at | username | full_name | avatar_url | website +--------------------------------------+------------+----------+-----------+------------+--------- + d4f0aa86-e6f6-41d1-bd32-391f077cf1b9 | | user1 | User 1 | | + 15d6811a-16ee-4fa2-9b18-b63085688be4 | | user2 | User 2 | | + 4e1010bb-eb37-4a4d-a05a-b0ee315c9d56 | | user3 | User 3 | | +(3 rows) + +postgres=> call auth.logout(); +CALL +postgres=> call auth.login_as_user('user1@example.com'); +NOTICE: set role authenticated; -- logging in as 'd4f0aa86-e6f6-41d1-bd32-391f077cf1b9' ('user1@example.com') +CALL +postgres=> update public.profiles set updated_at=now(); +UPDATE 1 -- authenticated users can update their own profile and see all of them +postgres=> table public.profiles; + id | updated_at | username | full_name | avatar_url | website +--------------------------------------+-------------------------------+----------+-----------+------------+--------- + 15d6811a-16ee-4fa2-9b18-b63085688be4 | | user1 | User 1 | | + 4e1010bb-eb37-4a4d-a05a-b0ee315c9d56 | | user2 | User 2 | | + d4f0aa86-e6f6-41d1-bd32-391f077cf1b9 | 2023-02-18 21:39:16.204612+00 | user3 | User 3 | | +(3 rows) + +postgres=> call auth.logout(); +CALL +postgres=> update public.profiles set updated_at=now(); +UPDATE 3 -- the 'postgres' role can update and see all profiles +postgres=> table public.profiles; + id | updated_at | username | full_name | avatar_url | website +--------------------------------------+-------------------------------+----------+-----------+------------+--------- + 15d6811a-16ee-4fa2-9b18-b63085688be4 | 2023-02-18 21:40:08.216324+00 | user1 | User 1 | | + 4e1010bb-eb37-4a4d-a05a-b0ee315c9d56 | 2023-02-18 21:40:08.216324+00 | user2 | User 2 | | + d4f0aa86-e6f6-41d1-bd32-391f077cf1b9 | 2023-02-18 21:40:08.216324+00 | user3 | User 3 | | +(3 rows) + +``` + +{{store.description }}
+{{selectedStore?.name}}
+24/7 × 365 | 1 business hour
24/7 × 365 | 1 business hour
24/7 × 365 | -| 2. High | 4 business hours
Monday - Friday | 2 business hours
Monday - Friday | 2 business hours
24/7 × 365 | +| 1. Urgent | 1 hour
24/7 × 365 | 1 hour
24/7 × 365 | 1 hour
24/7 × 365 | +| 2. High | 4 business hours
Monday - Friday | 2 business hours
Monday - Friday | 2 hours
24/7 × 365 | | 3. Normal | 1 business day
Monday - Friday | 1 business day
Monday - Friday | 12 business hours
Monday - Friday | | 4. Low | 2 business days
Monday - Friday | 2 business days
Monday - Friday | 1 business day
Monday - Friday | diff --git a/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/capacitor-map.png b/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/capacitor-map.png new file mode 100644 index 00000000000..be25c5f428f Binary files /dev/null and b/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/capacitor-map.png differ diff --git a/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/ionic-inputs.png b/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/ionic-inputs.png new file mode 100644 index 00000000000..60af6081d3e Binary files /dev/null and b/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/ionic-inputs.png differ diff --git a/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/ionic-map-marker.png b/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/ionic-map-marker.png new file mode 100644 index 00000000000..b2a989abfc2 Binary files /dev/null and b/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/ionic-map-marker.png differ diff --git a/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/ionic-marker-with-image.png b/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/ionic-marker-with-image.png new file mode 100644 index 00000000000..4b8cc6ebb98 Binary files /dev/null and b/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/ionic-marker-with-image.png differ diff --git a/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/ionic-nearby.png b/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/ionic-nearby.png new file mode 100644 index 00000000000..99a8b474cbf Binary files /dev/null and b/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/ionic-nearby.png differ diff --git a/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/postgis-ionic-angular.png b/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/postgis-ionic-angular.png new file mode 100644 index 00000000000..e3a9268ccf6 Binary files /dev/null and b/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/postgis-ionic-angular.png differ diff --git a/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/supabase-extensions.png b/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/supabase-extensions.png new file mode 100644 index 00000000000..4b32a04c7d8 Binary files /dev/null and b/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/supabase-extensions.png differ diff --git a/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/supabase-gis.png b/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/supabase-gis.png new file mode 100644 index 00000000000..fa4f448b407 Binary files /dev/null and b/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/supabase-gis.png differ diff --git a/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/supabase-project-setup.png b/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/supabase-project-setup.png new file mode 100644 index 00000000000..d946dbc3370 Binary files /dev/null and b/apps/www/public/images/blog/2023-02-28-geoqueries-postgis/supabase-project-setup.png differ diff --git a/examples/todo-list/nextjs-todo-list/.env.local.example b/examples/todo-list/nextjs-todo-list/.env.local.example new file mode 100644 index 00000000000..477da3d401d --- /dev/null +++ b/examples/todo-list/nextjs-todo-list/.env.local.example @@ -0,0 +1,3 @@ +# Update these with your Supabase details from your project settings > API +NEXT_PUBLIC_SUPABASE_URL=https://your-project.supabase.co +NEXT_PUBLIC_SUPABASE_ANON_KEY=your-anon-key \ No newline at end of file diff --git a/examples/todo-list/nextjs-todo-list/.eslintrc.json b/examples/todo-list/nextjs-todo-list/.eslintrc.json new file mode 100644 index 00000000000..bffb357a712 --- /dev/null +++ b/examples/todo-list/nextjs-todo-list/.eslintrc.json @@ -0,0 +1,3 @@ +{ + "extends": "next/core-web-vitals" +} diff --git a/examples/todo-list/nextjs-todo-list/.gitignore b/examples/todo-list/nextjs-todo-list/.gitignore new file mode 100644 index 00000000000..c87c9b392c0 --- /dev/null +++ b/examples/todo-list/nextjs-todo-list/.gitignore @@ -0,0 +1,36 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# next.js +/.next/ +/out/ + +# production +/build + +# misc +.DS_Store +*.pem + +# debug +npm-debug.log* +yarn-debug.log* +yarn-error.log* +.pnpm-debug.log* + +# local env files +.env*.local + +# vercel +.vercel + +# typescript +*.tsbuildinfo +next-env.d.ts diff --git a/examples/todo-list/nextjs-todo-list/README.md b/examples/todo-list/nextjs-todo-list/README.md new file mode 100644 index 00000000000..83ba22e28bd --- /dev/null +++ b/examples/todo-list/nextjs-todo-list/README.md @@ -0,0 +1,75 @@ +# Todo example using Supabase + +- Frontend: + - [Next.js](https://github.com/vercel/next.js) - a React framework for production. + - [Tailwind](https://tailwindcss.com/) for styling and layout. + - [Supabase.js](https://supabase.com/docs/library/getting-started) for user management and realtime data syncing. +- Backend: + - [app.supabase.com](https://app.supabase.com/): hosted Postgres database with restful API for usage with Supabase.js. + +## Deploy with Vercel + +The Vercel deployment will guide you through creating a Supabase account and project. After installation of the Supabase integration, all relevant environment variables will be set up so that the project is usable immediately after deployment 🚀 + +[](https://vercel.com/new/git/external?repository-url=https%3A%2F%2Fgithub.com%2Fsupabase%2Fexamples%2Ftree%2Fmain%2Fsupabase-js-v1%2Ftodo-list%2Fnextjs-todo-list&project-name=supabase-todo-list&repository-name=supabase-todo-list&demo-title=Todo%20list&demo-description=An%20example%20web%20app%20using%20Supabase%20and%20Next.js&demo-url=https%3A%2F%2Fsupabase-nextjs-todo-list.vercel.app&demo-image=https%3A%2F%2Fi.imgur.com%2FGJauPlN.png&integration-ids=oac_jUduyjQgOyzev1fjrW83NYOv&external-id=supabase-todo-list) + +## Build from scratch + +### 1. Create new project + +Sign up to Supabase - [https://app.supabase.com](https://app.supabase.com) and create a new project. Wait for your database to start. + +### 2. Run "Todo List" Quickstart + +Once your database has started, run the "Todo List" quickstart. Inside of your project, enter the `SQL editor` tab and scroll down until you see `TODO LIST: Build a basic todo list with Row Level Security`. + +### 3. Get the URL and Key + +Go to the Project Settings (the cog icon), open the API tab, and find your API URL and `anon` key, you'll need these in the next step. + +The `anon` key is your client-side API key. It allows "anonymous access" to your database, until the user has logged in. Once they have logged in, the keys will switch to the user's own login token. This enables row level security for your data. Read more about this [below](#postgres-row-level-security). + + + +**_NOTE_**: The `service_role` key has full access to your data, bypassing any security policies. These keys have to be kept secret and are meant to be used in server environments and never on a client or browser. + +## Supabase details + +### Postgres Row level security + +This project uses very high-level Authorization using Postgres' Role Level Security. +When you start a Postgres database on Supabase, we populate it with an `auth` schema, and some helper functions. +When a user logs in, they are issued a JWT with the role `authenticated` and their UUID. +We can use these details to provide fine-grained control over what each user can and cannot do. + +This is a trimmed-down schema, with the policies: + +```sql +create table todos ( + id bigint generated by default as identity primary key, + user_id uuid references auth.users not null, + task text check (char_length(task) > 3), + is_complete boolean default false, + inserted_at timestamp with time zone default timezone('utc'::text, now()) not null +); + +alter table todos enable row level security; + +create policy "Individuals can create todos." on todos for + insert with check (auth.uid() = user_id); + +create policy "Individuals can view their own todos. " on todos for + select using (auth.uid() = user_id); + +create policy "Individuals can update their own todos." on todos for + update using (auth.uid() = user_id); + +create policy "Individuals can delete their own todos." on todos for + delete using (auth.uid() = user_id); +``` + +## Authors + +- [Supabase](https://supabase.com) + +Supabase is open source. We'd love for you to follow along and get involved at https://github.com/supabase/supabase diff --git a/examples/todo-list/nextjs-todo-list/components/TodoList.tsx b/examples/todo-list/nextjs-todo-list/components/TodoList.tsx new file mode 100644 index 00000000000..6d060b5a964 --- /dev/null +++ b/examples/todo-list/nextjs-todo-list/components/TodoList.tsx @@ -0,0 +1,151 @@ +import { Database } from '@/lib/schema' +import { Session, useSupabaseClient } from '@supabase/auth-helpers-react' +import { useEffect, useState } from 'react' + +type Todos = Database['public']['Tables']['todos']['Row'] + +export default function TodoList({ session }: { session: Session }) { + const supabase = useSupabaseClient
Todo List.
+ + {!!errorText &&-
+ {todos.map((todo) => (
+