Skip to content

Latest commit

 

History

History
102 lines (79 loc) · 6.13 KB

File metadata and controls

102 lines (79 loc) · 6.13 KB

🏡 Home

Start a new Supabase project Create a Next.js app with the create-next-app CLI Query Supabase data from Next.js Server Components Create an OAuth app with GitHub Authenticate users with GitHub OAuth using Supabase and Next.js Client Components Refresh session cookie for Next.js Server Components with Middleware Restrict access to authenticated users with RLS policies Dynamically render UI based on user session with SSR in Next.js Client Components Implement Protected Routes for authenticated users with Supabase Auth Generate TypeScript definitions from PostgreSQL schema with Supabase CLI Setup a Foreign Key relationship between PostgreSQL tables Automatically generate a profile for every user with PostgreSQL Function Triggers Run authenticated Server-side mutations with Next.js Server Actions Create a PostgreSQL join table in Supabase Studio Implement dynamic buttons with Next.js Client Components Declare global union types with Typescript Implement Optimistic UI with the Next.js useTransition hook Dynamically update UI with Database changes using Supabase Realtime Style a Twitter clone with Tailwind CSS Deploy Next.js App Router project to production with Vercel

Automatically generate a profile for every user with PostgreSQL Function Triggers

📹 Video

Supabase has an auth.users table that contains information about our user and their session. We want to display the user's name, username and avatar alongside their tweets, but the auth.users table cannot be publicly accessible, as it contains sensitive information.

In this lesson, we create a new table called profiles and populate it with the data we want to display from the auth.users table. Additionally, we set up a PostgreSQL Function and Trigger to create a new profile for any user added to the auth.users table.

Lastly, we create an RLS policy for the profiles table to enable read access, and re-generate our TypeScript definitions file to contain our new table.

Code Snippets

Create profiles table

create table public.profiles (
  id uuid not null references auth.users on delete cascade primary key,
  name text not null,
  username text not null,
  avatar_url text not null
);

Enable Row Level Security

alter table public.profiles enable row level security;

Enable read access with RLS policy

create policy "anyone can select profiles" ON "public"."profiles"
as permissive for select
to public
using (true);

Create PostgreSQL Function to create profile

create function public.create_profile_for_user()
returns trigger
language plpgsql
security definer set search_path = public
as $$
begin
  insert into public.profiles (id, name, username, avatar_url)
  values (
    new.id,
    new.raw_user_meta_data->'name',
    new.raw_user_meta_data->'user_name',
    new.raw_user_meta_data->'avatar_url'
  );
  return new;
end;
$$;

Create PostgreSQL Trigger to create profile

create trigger on_auth_user_created
  after insert on auth.users
  for each row execute procedure public.create_profile_for_user();

Resources


👉 Next lesson


Enjoying the course? Follow Jon Meyers on Twitter and subscribe to the YouTube channel.