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 -- Run this in Supabase SQL Editor
-- Option 1: Add INSERT policy for self-signup -- Simplified trigger - only creates profile, no subscription insert
CREATE POLICY "user_subscriptions_insert_self" ON user_subscriptions -- (subscription is handled client-side after signup)
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
CREATE OR REPLACE FUNCTION public.handle_new_user() CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS trigger AS $$ RETURNS trigger AS $$
BEGIN BEGIN
-- Create profile -- Create profile only
INSERT INTO public.profiles (id, username, avatar_url) INSERT INTO public.profiles (id, username, avatar_url)
VALUES ( VALUES (
new.id, new.id,
@@ -21,13 +15,16 @@ BEGIN
) )
ON CONFLICT (id) DO NOTHING; ON CONFLICT (id) DO NOTHING;
-- Create subscription with starter plan RETURN new;
INSERT INTO public.user_subscriptions (user_id, plan_id) EXCEPTION WHEN OTHERS THEN
SELECT -- Log but don't fail user creation
new.id, RAISE WARNING 'handle_new_user failed: %', SQLERRM;
(SELECT id FROM subscription_plans WHERE name = 'starter' LIMIT 1)
ON CONFLICT (user_id) DO NOTHING;
RETURN new; RETURN new;
END; END;
$$ LANGUAGE plpgsql SECURITY DEFINER; $$ 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
);