Skip to content

Commit 374051e

Browse files
authored
fix sign in bug (#48)
* fix sign in bug * fix redirect
1 parent d32e046 commit 374051e

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

components/auth/dialog.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ const useAuthHandlers = (options: AuthHandlersOptions): UseAuthHandlers => {
320320
) => {
321321
try {
322322
setLoadingProvider(provider);
323-
await signIn.social({ provider, callbackURL: "/" });
323+
await signIn.social({ provider, callbackURL: window.location.pathname });
324324
} catch {
325325
toast.error(`Failed to sign in with ${getProviderLabel(provider)}`);
326326
setLoadingProvider(null);

lib/auth.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { betterAuth } from "better-auth";
22
import { drizzleAdapter } from "better-auth/adapters/drizzle";
33
import { anonymous, genericOAuth } from "better-auth/plugins";
4+
import { eq } from "drizzle-orm";
45
import { db } from "./db";
56
import {
67
accounts,
8+
integrations,
79
sessions,
810
users,
911
verifications,
@@ -51,7 +53,47 @@ function getBaseURL() {
5153

5254
// Build plugins array conditionally
5355
const plugins = [
54-
anonymous(),
56+
anonymous({
57+
async onLinkAccount(data) {
58+
// When an anonymous user links to a real account, migrate their data
59+
const fromUserId = data.anonymousUser.user.id;
60+
const toUserId = data.newUser.user.id;
61+
62+
console.log(
63+
`[Anonymous Migration] Migrating from user ${fromUserId} to ${toUserId}`
64+
);
65+
66+
try {
67+
// Migrate workflows
68+
await db
69+
.update(workflows)
70+
.set({ userId: toUserId })
71+
.where(eq(workflows.userId, fromUserId));
72+
73+
// Migrate workflow executions
74+
await db
75+
.update(workflowExecutions)
76+
.set({ userId: toUserId })
77+
.where(eq(workflowExecutions.userId, fromUserId));
78+
79+
// Migrate integrations
80+
await db
81+
.update(integrations)
82+
.set({ userId: toUserId })
83+
.where(eq(integrations.userId, fromUserId));
84+
85+
console.log(
86+
`[Anonymous Migration] Successfully migrated data from ${fromUserId} to ${toUserId}`
87+
);
88+
} catch (error) {
89+
console.error(
90+
"[Anonymous Migration] Error migrating user data:",
91+
error
92+
);
93+
throw error;
94+
}
95+
},
96+
}),
5597
...(process.env.VERCEL_CLIENT_ID
5698
? [
5799
genericOAuth({

0 commit comments

Comments
 (0)