Skip to content

Commit 3e1b5e0

Browse files
committed
handle the case where the google auth conflicts with an existing user with the same email address
1 parent 7e50b54 commit 3e1b5e0

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

apps/webapp/app/models/user.server.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ import {
99
} from "~/services/dashboardPreferences.server";
1010
export type { User } from "@trigger.dev/database";
1111
import { assertEmailAllowed } from "~/utils/email";
12+
import { logger } from "~/services/logger.server";
13+
14+
export class AuthConflictError extends Error {
15+
constructor(
16+
message: string,
17+
public readonly existingEmailUserId: string,
18+
public readonly existingAuthUserId: string
19+
) {
20+
super(message);
21+
this.name = "AuthConflictError";
22+
}
23+
}
24+
1225
type FindOrCreateMagicLink = {
1326
authenticationMethod: "MAGIC_LINK";
1427
email: string;
@@ -208,13 +221,12 @@ export async function findOrCreateGoogleUser({
208221
});
209222

210223
if (existingEmailUser && !existingUser) {
211-
// Link existing email account to Google auth
224+
// Link existing email account to Google auth, preserving original authenticationMethod
212225
const user = await prisma.user.update({
213226
where: {
214227
email,
215228
},
216229
data: {
217-
authenticationMethod: "GOOGLE",
218230
authenticationProfile: authProfile,
219231
authenticationExtraParams: authExtraParams,
220232
avatarUrl,
@@ -229,12 +241,34 @@ export async function findOrCreateGoogleUser({
229241
}
230242

231243
if (existingEmailUser && existingUser) {
232-
// User already linked to Google, update profile info
244+
// Check if email user and auth user are the same
245+
if (existingEmailUser.id !== existingUser.id) {
246+
// Different users: email is taken by one user, Google auth belongs to another
247+
logger.error(
248+
`Google auth conflict: Google ID ${authenticationProfile.id} belongs to user ${existingUser.id} but email ${email} is taken by user ${existingEmailUser.id}`,
249+
{
250+
email,
251+
existingEmailUserId: existingEmailUser.id,
252+
existingAuthUserId: existingUser.id,
253+
authIdentifier,
254+
}
255+
);
256+
257+
return {
258+
user: existingUser,
259+
isNewUser: false,
260+
};
261+
}
262+
263+
// Same user: update all profile fields
233264
const user = await prisma.user.update({
234265
where: {
235266
id: existingUser.id,
236267
},
237268
data: {
269+
email,
270+
displayName,
271+
name,
238272
avatarUrl,
239273
authenticationProfile: authProfile,
240274
authenticationExtraParams: authExtraParams,

0 commit comments

Comments
 (0)