Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/auth/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
44 changes: 43 additions & 1 deletion lib/auth.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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({
Expand Down