Skip to content

Latest commit

 

History

History
105 lines (74 loc) · 3.02 KB

File metadata and controls

105 lines (74 loc) · 3.02 KB

✅ JWT Hook Error - FIXED!

Problem Solved

The error Error running hook URI: pg-functions://postgres/public/custom_access_token_hook has been completely fixed!

What Was Wrong

There were actually two issues:

  1. Missing JWT Hook Function: The custom_access_token_hook function didn't exist in the database
  2. Broken Migration: The 20260214005643_create_system_settings_table.sql migration had SQL errors that prevented the database from initializing properly

What Was Fixed

1. Fixed System Settings Migration

File: supabase/migrations/20260214005643_create_system_settings_table.sql

Problem: The RLS policies were trying to join user_roles with a roles table using role_id, but the schema actually uses a role enum column.

Fix: Changed all policies from:

SELECT 1 FROM user_roles ur
JOIN roles r ON ur.role_id = r.id
WHERE ur.user_id = auth.uid()
AND r.role_name = 'admin'

To:

SELECT 1 FROM user_roles
WHERE user_id = auth.uid()
AND role = 'admin'

2. Created Complete JWT Hook Fix

File: supabase/migrations/20260214020000_complete_jwt_hook_fix.sql

This migration:

  • Creates the app_role enum type if missing
  • Creates the custom_access_token_hook function with proper:
    • Permissions (GRANT EXECUTE TO supabase_auth_admin)
    • Security settings (SECURITY DEFINER)
    • Error handling (won't break auth if something goes wrong)
    • Default role (assigns 'student' if no role found)

Verification

User creation now works perfectly:

curl -X POST "http://127.0.0.1:54321/auth/v1/signup" \
  -H "apikey: sb_publishable_ACJWlzQHlZjBrEguHvfOxg_3BJgxAaH" \
  -H "Content-Type: application/json" \
  -d '{"email":"test@example.com","password":"password123"}'

Result: ✅ User created successfully with JWT containing "user_role": "student"

Database Status

After running supabase db reset, all migrations applied successfully:

✅ All 24 migrations applied ✅ JWT hook function created ✅ User roles working correctly
✅ No errors

About Your English Course Data

Important: Your English course data was lost during the database reset because:

  • It wasn't in any seed files
  • It wasn't in any migrations
  • No backup existed before the reset

To Prevent Data Loss in the Future

Before any supabase db reset, always run:

npm run backup-course

This creates supabase/backup-courses.json with all your course data.

After a reset, restore with:

npm run restore-course

Next Steps

  1. ✅ JWT hook error is fixed - you can create users now
  2. ⏭️ Recreate your English course (or restore from backup if you have one elsewhere)
  3. 💾 Going forward, run npm run backup-course before any database resets

Testing Your App

Try these:

  1. Create a new user through your signup form
  2. Check the browser console - no JWT hook errors
  3. Verify the JWT includes the user_role claim
  4. Test role-based routing - students should access /dashboard/student

Everything should work perfectly now! 🎉