From b0698621a83e72ca513058c7dd761e4fe9985618 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Wed, 26 Nov 2025 14:30:06 -0600 Subject: [PATCH 1/2] fix sign in bug --- lib/auth.ts | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/auth.ts b/lib/auth.ts index 9651aba3e..9be288e84 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -1,9 +1,11 @@ import { betterAuth } from "better-auth"; import { drizzleAdapter } from "better-auth/adapters/drizzle"; import { anonymous, genericOAuth } from "better-auth/plugins"; +import { eq } from "drizzle-orm"; import { db } from "./db"; import { accounts, + integrations, sessions, users, verifications, @@ -51,7 +53,47 @@ function getBaseURL() { // Build plugins array conditionally const plugins = [ - anonymous(), + anonymous({ + async onLinkAccount(data) { + // When an anonymous user links to a real account, migrate their data + const fromUserId = data.anonymousUser.user.id; + const toUserId = data.newUser.user.id; + + console.log( + `[Anonymous Migration] Migrating from user ${fromUserId} to ${toUserId}` + ); + + try { + // Migrate workflows + await db + .update(workflows) + .set({ userId: toUserId }) + .where(eq(workflows.userId, fromUserId)); + + // Migrate workflow executions + await db + .update(workflowExecutions) + .set({ userId: toUserId }) + .where(eq(workflowExecutions.userId, fromUserId)); + + // Migrate integrations + await db + .update(integrations) + .set({ userId: toUserId }) + .where(eq(integrations.userId, fromUserId)); + + console.log( + `[Anonymous Migration] Successfully migrated data from ${fromUserId} to ${toUserId}` + ); + } catch (error) { + console.error( + "[Anonymous Migration] Error migrating user data:", + error + ); + throw error; + } + }, + }), ...(process.env.VERCEL_CLIENT_ID ? [ genericOAuth({ From 228be610c6c13e35319a1fef4dd36b8c6cdea0c9 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Wed, 26 Nov 2025 14:33:00 -0600 Subject: [PATCH 2/2] fix redirect --- components/auth/dialog.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/auth/dialog.tsx b/components/auth/dialog.tsx index af7dc0e1e..11a3ba13b 100644 --- a/components/auth/dialog.tsx +++ b/components/auth/dialog.tsx @@ -320,7 +320,7 @@ const useAuthHandlers = (options: AuthHandlersOptions): UseAuthHandlers => { ) => { try { setLoadingProvider(provider); - await signIn.social({ provider, callbackURL: "/" }); + await signIn.social({ provider, callbackURL: window.location.pathname }); } catch { toast.error(`Failed to sign in with ${getProviderLabel(provider)}`); setLoadingProvider(null);