Database
FlareStack uses Supabase as its database. It's a popular choice for modern web applications due to its scalability and real-time capabilities. Supabase offers features like authentication, storage, and real-time subscriptions, making it a comprehensive backend solution.
User Profiles
Within the Supabase SQL Editor, run this query to add a profiles table:
sql-- Create the profiles table in the public schema CREATE TABLE public.profiles ( id UUID PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE, name TEXT, email TEXT, image TEXT, customer_id TEXT, price_id TEXT, has_access BOOLEAN DEFAULT false, created_at TIMESTAMP WITH TIME ZONE DEFAULT (now() AT TIME ZONE 'UTC'), updated_at TIMESTAMP WITH TIME ZONE DEFAULT (now() AT TIME ZONE 'UTC') ); -- Create a function to update the updated_at timestamp CREATE OR REPLACE FUNCTION public.update_updated_at() RETURNS TRIGGER LANGUAGE plpgsql SET search_path TO public, pg_catalog AS $$ BEGIN NEW.updated_at = timezone('UTC', now()); RETURN NEW; END; $$; -- Create a trigger to automatically update the updated_at column CREATE TRIGGER update_profiles_updated_at BEFORE UPDATE ON public.profiles FOR EACH ROW EXECUTE FUNCTION update_updated_at(); -- Create a function to automatically add a profile on signup CREATE OR REPLACE FUNCTION public.handle_new_user() RETURNS TRIGGER LANGUAGE plpgsql SECURITY DEFINER SET search_path TO public, pg_catalog AS $$ BEGIN INSERT INTO public.profiles (id, email, name, image, created_at, updated_at) VALUES ( NEW.id, NEW.email, COALESCE(NEW.raw_user_meta_data->>'full_name', NEW.raw_user_meta_data->>'name'), NEW.raw_user_meta_data->>'avatar_url', timezone('UTC', now()), timezone('UTC', now()) ); RETURN NEW; END; $$; -- Create a trigger to call the handle_new_user function on signup CREATE TRIGGER on_auth_user_created AFTER INSERT ON auth.users FOR EACH ROW EXECUTE FUNCTION public.handle_new_user();
RLS Policies
Row Level Security (RLS) policies are crucial for controlling access to data in Supabase. These policies define who can perform what actions on specific rows in a table.
sql-- Enable Row Level Security ALTER TABLE public.profiles ENABLE ROW LEVEL SECURITY; -- Create a policy to allow users to read their own data CREATE POLICY read_own_profile_data ON public.profiles FOR SELECT USING (auth.uid() = id); -- Create a policy to allow users to update their own data CREATE POLICY update_own_profile_data ON public.profiles FOR UPDATE USING (auth.uid() = id); -- Create a policy to allow users to insert their own data CREATE POLICY insert_own_profile_data ON public.profiles FOR INSERT WITH CHECK (auth.uid() = id); -- Create a policy to allow users to delete their own data CREATE POLICY delete_own_profile_data ON public.profiles FOR DELETE USING (auth.uid() = id);
Supabase Security Advisor Warnings
Update the function's search path to eliminate warnings:
sqlCREATE OR REPLACE FUNCTION public.update_updated_at() RETURNS TRIGGER LANGUAGE plpgsql SET search_path TO public, pg_catalog AS $$ BEGIN NEW.updated_at = timezone('UTC', now()); RETURN NEW; END; $$;
Adding Additional Columns
Optional: You now have a user profile table that you can use to add other metadata within your application.
sqlALTER TABLE profiles ADD COLUMN payment_tier TEXT;