From c47c933ce15266a97b43f4062e1c91aa42972e98 Mon Sep 17 00:00:00 2001 From: OllyNicholass Date: Tue, 23 Jul 2024 20:53:03 +0100 Subject: [PATCH] refactor(#22): add PP with @jcreek Breaking version of customer stripe sync --- src/lib/components/SignIn.svelte | 64 +++++++++++++++++-- src/lib/components/SignUp.svelte | 32 +--------- src/lib/utils/supabase/admin.ts | 11 +++- src/routes/+layout.svelte | 2 +- ...0240723183331_customer_rls_user_update.sql | 40 ++++++++++++ 5 files changed, 108 insertions(+), 41 deletions(-) create mode 100644 supabase/migrations/20240723183331_customer_rls_user_update.sql diff --git a/src/lib/components/SignIn.svelte b/src/lib/components/SignIn.svelte index 8ad7d36..a7e7ac8 100644 --- a/src/lib/components/SignIn.svelte +++ b/src/lib/components/SignIn.svelte @@ -1,7 +1,11 @@
diff --git a/src/lib/utils/supabase/admin.ts b/src/lib/utils/supabase/admin.ts index a5a53eb..cdaff7f 100644 --- a/src/lib/utils/supabase/admin.ts +++ b/src/lib/utils/supabase/admin.ts @@ -155,8 +155,15 @@ const createOrRetrieveCustomer = async ({ email, uuid }: { email: string; uuid: console.warn(`Supabase customer record was missing. A new record was created.`); // If Supabase has no record, create a new record and return Stripe customer ID - const upsertedStripeCustomer = await upsertCustomerToSupabase(uuid, stripeIdToInsert); - if (!upsertedStripeCustomer) throw new Error('Supabase customer record creation failed.'); + try { + const upsertedStripeCustomer = await upsertCustomerToSupabase(uuid, stripeIdToInsert); + + if (!upsertedStripeCustomer) throw new Error('Supabase customer record creation failed.'); + return upsertedStripeCustomer; + } catch (error) { + console.error(`Supabase customer record creation failed: ${error}`); + return null; + } return upsertedStripeCustomer; } diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index fc71e3b..2a95ec3 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -147,7 +147,7 @@ {/if} diff --git a/supabase/migrations/20240723183331_customer_rls_user_update.sql b/supabase/migrations/20240723183331_customer_rls_user_update.sql new file mode 100644 index 0000000..ebf24e2 --- /dev/null +++ b/supabase/migrations/20240723183331_customer_rls_user_update.sql @@ -0,0 +1,40 @@ +-- TESTS FOR RLS policy +create policy "Enable update for users based on id" +on "public"."customers" +as permissive +for update +to public +using ((( SELECT auth.uid() AS uid) = id)) +with check ((( SELECT auth.uid() AS uid) = id)); + +create policy "Enable insert for users based on id" +on "public"."customers" +as permissive +for insert +to public +with check ((( SELECT auth.uid() AS uid) = id)); + +create policy "DEMO Enable insert for users based on id" +on "public"."customers" +as permissive +for insert +to public +with check (true); + +create policy "DEMO Enable update for users based on id" +on "public"."customers" +as permissive +for update +to public +using (true) +with check (true); + + + +GRANT USAGE ON SCHEMA public TO anon, authenticated, service_role; +GRANT ALL ON ALL TABLES IN SCHEMA public TO anon, authenticated, service_role; +GRANT ALL ON ALL ROUTINES IN SCHEMA public TO anon, authenticated, service_role; +GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO anon, authenticated, service_role; +ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON TABLES TO anon, authenticated, service_role; +ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON ROUTINES TO anon, authenticated, service_role; +ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO anon, authenticated, service_role; \ No newline at end of file