Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I integrate Supabase Auth with Row Level Security for Laravel


How do I integrate Supabase Auth with Row Level Security for Laravel


To integrate Supabase Auth with Row Level Security (RLS) for Laravel, follow these steps:

Step 1: Set Up Supabase Project

1. Create a Supabase Project: Go to the Supabase dashboard and create a new project. This will give you a Postgres database and other features like authentication and APIs.

Step 2: Set Up Laravel Project

1. Create a Laravel Project: Use Composer to create a new Laravel project:

bash
   composer create-project laravel/laravel example-app
   

2. Install Laravel Breeze: Install Laravel Breeze for authentication:

bash
   composer require laravel/breeze --dev
   php artisan breeze:install
   

Step 3: Configure Postgres Database Connection

1. Update Database Configuration: Update the Postgres database connection in `config/database.php` to match your Supabase project:

php
   'pgsql' => [
       'driver' => 'pgsql',
       'url' => env('DATABASE_URL'),
       'host' => env('DB_HOST', '127.0.0.1'),
       'port' => env('DB_PORT', '5432'),
       'database' => env('DB_DATABASE', 'forge'),
       'username' => env('DB_USERNAME', 'forge'),
       'password' => env('DB_PASSWORD', ''),
       'charset' => 'utf8',
       'prefix' => '',
       'prefix_indexes' => true,
       'search_path' => 'public', // Update this to match your Supabase schema
       'sslmode' => 'prefer',
   ],
   

2. Update Environment Variables: Update the environment variables in `.env` to match your Supabase project:

ini
   DB_CONNECTION=pgsql
   DATABASE_URL=postgres://your_username:your_password@your_host:5432/your_database
   

Step 4: Enable RLS on Tables

1. Enable RLS on Tables: Enable RLS on the tables you want to secure. For example, enable RLS on the `profiles` table:

sql
   ALTER TABLE profiles ENABLE ROW LEVEL SECURITY;
   

Step 5: Create Policies

1. Create Policies: Create policies to restrict access to tables based on user roles. For example, create a policy to restrict access to the `profiles` table based on the `user_id` column:

sql
   CREATE POLICY "Users can only view their own profile" 
   ON profiles
   FOR SELECT
   USING (auth.uid() = user_id);
   

Step 6: Use Supabase Auth with Laravel

1. Use Supabase Auth: Use Supabase Auth to authenticate users and manage their sessions. You can use the `auth.jwt()` function to check for Multi-Factor Authentication (MFA) and restrict access to certain actions:

sql
   CREATE POLICY "Restrict updates" 
   ON profiles
   AS RESTRICTIVE
   FOR UPDATE
   TO authenticated
   USING (auth.jwt()->>'aal' = 'aal2');
   

Step 7: Integrate Supabase Auth with Laravel

1. Integrate Supabase Auth with Laravel: Integrate Supabase Auth with Laravel by using the Supabase client library to handle user authentication and session management. You can use the `auth.uid()` function to get the user ID and the `auth.jwt()` function to get the JWT token:

php
   use Supabase\Client;

   $supabase = new Client([
       'supabaseUrl' => 'https://your-supabase-url.supabase.co',
       'supabaseKey' => 'your-supabase-anon-key',
   ]);

   $user = $supabase->auth()->getUser();
   $userId = $user['id'];
   $jwtToken = $user['jwt'];
   

By following these steps, you can integrate Supabase Auth with Row Level Security for Laravel, ensuring that your application's data is securely accessed and managed based on user roles and permissions.

Citations:
[1] https://supabase.com/docs/guides/database/postgres/row-level-security
[2] https://www.reddit.com/r/sveltejs/comments/utlbua/sveltekit_endpoints_and_supabase_with_rls/
[3] https://supabase.com/docs/guides/getting-started/quickstarts/laravel
[4] https://www.youtube.com/watch?v=dg1suKt-7D4
[5] https://supabase.com/docs/guides/auth