fix: Update handle_new_user trigger for reliable registration

- Simplified trigger to only create profile
- Added exception handling to prevent user creation failures
- Subscription created client-side after successful signup
This commit is contained in:
2025-12-26 23:05:30 +01:00
parent 30a716f3e2
commit 9e2abb0aa3

View File

@@ -1,18 +1,12 @@
-- Fix: Allow users to insert their own subscription on signup
-- Fix for user registration trigger
-- Run this in Supabase SQL Editor
-- Option 1: Add INSERT policy for self-signup
CREATE POLICY "user_subscriptions_insert_self" ON user_subscriptions
FOR INSERT WITH CHECK (
(SELECT auth.uid()) = user_id
);
-- Option 2 (better): Extend the existing handle_new_user trigger
-- This automatically creates subscription when user registers
-- Simplified trigger - only creates profile, no subscription insert
-- (subscription is handled client-side after signup)
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS trigger AS $$
BEGIN
-- Create profile
-- Create profile only
INSERT INTO public.profiles (id, username, avatar_url)
VALUES (
new.id,
@@ -21,13 +15,16 @@ BEGIN
)
ON CONFLICT (id) DO NOTHING;
-- Create subscription with starter plan
INSERT INTO public.user_subscriptions (user_id, plan_id)
SELECT
new.id,
(SELECT id FROM subscription_plans WHERE name = 'starter' LIMIT 1)
ON CONFLICT (user_id) DO NOTHING;
RETURN new;
EXCEPTION WHEN OTHERS THEN
-- Log but don't fail user creation
RAISE WARNING 'handle_new_user failed: %', SQLERRM;
RETURN new;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- Allow users to insert their own subscription
CREATE POLICY "user_subscriptions_insert_self" ON user_subscriptions
FOR INSERT WITH CHECK (
(SELECT auth.uid()) = user_id
);