{"id":1106,"date":"2024-01-31T22:39:54","date_gmt":"2024-02-01T03:39:54","guid":{"rendered":"https:\/\/sunapi386.ca\/wordpress\/?p=1106"},"modified":"2024-01-31T22:39:54","modified_gmt":"2024-02-01T03:39:54","slug":"supabase-auth-and-prisma-integration","status":"publish","type":"post","link":"https:\/\/sunapi386.ca\/wordpress\/supabase-auth-and-prisma-integration\/","title":{"rendered":"Supabase Auth and Prisma Integration"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Why This Integration?<\/h2>\n\n\n\n<p>Integrating Supabase Auth with Prisma offers a robust solution for managing user authentication alongside extended user profiles within a single application. While Supabase Auth provides a secure and scalable authentication system, it limits user-related data to basic information. By synchronizing <code>auth.users<\/code> with a custom <code>public.users<\/code> table managed by Prisma, we gain the flexibility to extend user profiles with additional fields and maintain referential integrity through foreign keys, enhancing our application&#8217;s data model and capabilities.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How It Works<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Overview<\/h3>\n\n\n\n<p>This integration automatically mirrors entries from Supabase&#8217;s <code>auth.users<\/code> table to a custom <code>public.users<\/code> table in our database. This synchronization allows us to extend the user model with additional information (e.g., user settings) and establish foreign key relationships with other tables, all managed through Prisma.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Setting Up Prisma<\/h3>\n\n\n\n<p>Define your <code>User<\/code> model in <code>schema.prisma<\/code> to mirror Supabase&#8217;s <code>auth.users<\/code> and include additional fields:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>model User {\n  id        String   @id @default(uuid()) @map(\"id\")\n  email     String   @unique\n  settings  Json?\n  createdAt DateTime @default(now()) @map(\"created_at\")\n  updatedAt DateTime @updatedAt @map(\"updated_at\")\n\n  @@map(\"users\")\n}\n\n\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Creating a PostgreSQL Trigger in Supabase<\/h3>\n\n\n\n<p>Implement a PostgreSQL trigger within Supabase to sync every new or updated entry in <code>auth.users<\/code> with <code>public.users<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE OR REPLACE FUNCTION public.handle_user_sync()\nRETURNS TRIGGER AS $$\nBEGIN\n  IF TG_OP = 'INSERT' THEN\n    INSERT INTO public.users (id, email, created_at, updated_at)\n    VALUES (NEW.id, NEW.email, NOW(), NOW())\n    ON CONFLICT (id) DO NOTHING;\n    RETURN NEW;\n  ELSIF TG_OP = 'UPDATE' THEN\n    UPDATE public.users\n    SET email = NEW.email, updated_at = NOW()\n    WHERE id = NEW.id;\n    RETURN NEW;\n  END IF;\nEND;\n$$ LANGUAGE plpgsql;\n\nCREATE TRIGGER sync_auth_users\nAFTER INSERT OR UPDATE ON auth.users\nFOR EACH ROW EXECUTE FUNCTION public.handle_user_sync();\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Managing User Data with Prisma<\/h3>\n\n\n\n<p>With the synchronization in place, use Prisma to manage <code>public.users<\/code>, adding any additional user-related data or settings as needed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Security and Performance<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Ensure sensitive user information is securely handled and access-controlled.<\/li>\n\n\n\n<li>Monitor the performance impact of the database trigger, adjusting as necessary.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Testing<\/h3>\n\n\n\n<p>Before deployment, thoroughly test the integration in a development environment to confirm that user data syncs correctly and that your application logic functions as expected.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>This README outlines the integration of Supabase Auth with Prisma for enhanced user data management. By leveraging the strengths of both platforms, we create a powerful foundation for building sophisticated and scalable applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Why This Integration? Integrating Supabase Auth with Prisma offers a robust solution for managing user authentication alongside extended user profiles within a single application. While Supabase Auth provides a secure and scalable authentication system, it limits user-related data to basic information. By synchronizing auth.users with a custom public.users table managed by Prisma, we gain the &hellip; <a href=\"https:\/\/sunapi386.ca\/wordpress\/supabase-auth-and-prisma-integration\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Supabase Auth and Prisma Integration<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1106","post","type-post","status-publish","format-standard","hentry","category-thoughts"],"_links":{"self":[{"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/posts\/1106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/comments?post=1106"}],"version-history":[{"count":1,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/posts\/1106\/revisions"}],"predecessor-version":[{"id":1107,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/posts\/1106\/revisions\/1107"}],"wp:attachment":[{"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/media?parent=1106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/categories?post=1106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/tags?post=1106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}