Skip to content

Commit f6b7c15

Browse files
authored
improvement(oauth): added random identifier in unused accountId to bypass betterauth unique constraint (#2807)
* improvement(oauth): added random identifier in unnused accountId to bypass betterauth unique constraint * ack pr comments
1 parent 70ed19f commit f6b7c15

File tree

5 files changed

+10270
-23
lines changed

5 files changed

+10270
-23
lines changed

apps/sim/lib/auth/auth.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ export const auth = betterAuth({
656656
const now = new Date()
657657

658658
return {
659-
id: profile.id.toString(),
659+
id: `${profile.id.toString()}-${crypto.randomUUID()}`,
660660
name: profile.name || profile.login,
661661
email: profile.email,
662662
image: profile.avatar_url,
@@ -962,7 +962,7 @@ export const auth = betterAuth({
962962
const now = new Date()
963963

964964
return {
965-
id: uniqueId,
965+
id: `${uniqueId}-${crypto.randomUUID()}`,
966966
name: 'Wealthbox User',
967967
email: `${uniqueId}@wealthbox.user`,
968968
emailVerified: false,
@@ -1016,7 +1016,7 @@ export const auth = betterAuth({
10161016
const user = data.data
10171017

10181018
return {
1019-
id: user.id.toString(),
1019+
id: `${user.id.toString()}-${crypto.randomUUID()}`,
10201020
name: user.name,
10211021
email: user.email,
10221022
emailVerified: user.activated,
@@ -1108,7 +1108,7 @@ export const auth = betterAuth({
11081108
})
11091109

11101110
return {
1111-
id: data.user_id || data.hub_id.toString(),
1111+
id: `${data.user_id || data.hub_id.toString()}-${crypto.randomUUID()}`,
11121112
name: data.user || 'HubSpot User',
11131113
email: data.user || `hubspot-${data.hub_id}@hubspot.com`,
11141114
emailVerified: true,
@@ -1162,7 +1162,7 @@ export const auth = betterAuth({
11621162
const data = await response.json()
11631163

11641164
return {
1165-
id: data.user_id || data.sub,
1165+
id: `${data.user_id || data.sub}-${crypto.randomUUID()}`,
11661166
name: data.name || 'Salesforce User',
11671167
email: data.email || `salesforce-${data.user_id}@salesforce.com`,
11681168
emailVerified: data.email_verified || true,
@@ -1221,7 +1221,7 @@ export const auth = betterAuth({
12211221
const now = new Date()
12221222

12231223
return {
1224-
id: profile.data.id,
1224+
id: `${profile.data.id}-${crypto.randomUUID()}`,
12251225
name: profile.data.name || 'X User',
12261226
email: `${profile.data.username}@x.com`,
12271227
image: profile.data.profile_image_url,
@@ -1295,7 +1295,7 @@ export const auth = betterAuth({
12951295
const now = new Date()
12961296

12971297
return {
1298-
id: profile.account_id,
1298+
id: `${profile.account_id}-${crypto.randomUUID()}`,
12991299
name: profile.name || profile.display_name || 'Confluence User',
13001300
email: profile.email || `${profile.account_id}@atlassian.com`,
13011301
image: profile.picture || undefined,
@@ -1406,7 +1406,7 @@ export const auth = betterAuth({
14061406
const now = new Date()
14071407

14081408
return {
1409-
id: profile.account_id,
1409+
id: `${profile.account_id}-${crypto.randomUUID()}`,
14101410
name: profile.name || profile.display_name || 'Jira User',
14111411
email: profile.email || `${profile.account_id}@atlassian.com`,
14121412
image: profile.picture || undefined,
@@ -1456,7 +1456,7 @@ export const auth = betterAuth({
14561456
const now = new Date()
14571457

14581458
return {
1459-
id: data.id,
1459+
id: `${data.id}-${crypto.randomUUID()}`,
14601460
name: data.email ? data.email.split('@')[0] : 'Airtable User',
14611461
email: data.email || `${data.id}@airtable.user`,
14621462
emailVerified: !!data.email,
@@ -1505,7 +1505,7 @@ export const auth = betterAuth({
15051505
const now = new Date()
15061506

15071507
return {
1508-
id: profile.bot?.owner?.user?.id || profile.id,
1508+
id: `${profile.bot?.owner?.user?.id || profile.id}-${crypto.randomUUID()}`,
15091509
name: profile.name || profile.bot?.owner?.user?.name || 'Notion User',
15101510
email: profile.person?.email || `${profile.id}@notion.user`,
15111511
emailVerified: !!profile.person?.email,
@@ -1572,7 +1572,7 @@ export const auth = betterAuth({
15721572
const now = new Date()
15731573

15741574
return {
1575-
id: data.id,
1575+
id: `${data.id}-${crypto.randomUUID()}`,
15761576
name: data.name || 'Reddit User',
15771577
email: `${data.name}@reddit.user`,
15781578
image: data.icon_img || undefined,
@@ -1644,7 +1644,7 @@ export const auth = betterAuth({
16441644
const viewer = data.viewer
16451645

16461646
return {
1647-
id: viewer.id,
1647+
id: `${viewer.id}-${crypto.randomUUID()}`,
16481648
email: viewer.email,
16491649
name: viewer.name,
16501650
emailVerified: true,
@@ -1707,7 +1707,7 @@ export const auth = betterAuth({
17071707
const data = await response.json()
17081708

17091709
return {
1710-
id: data.account_id,
1710+
id: `${data.account_id}-${crypto.randomUUID()}`,
17111711
email: data.email,
17121712
name: data.name?.display_name || data.email,
17131713
emailVerified: data.email_verified || false,
@@ -1758,7 +1758,7 @@ export const auth = betterAuth({
17581758
const now = new Date()
17591759

17601760
return {
1761-
id: profile.gid,
1761+
id: `${profile.gid}-${crypto.randomUUID()}`,
17621762
name: profile.name || 'Asana User',
17631763
email: profile.email || `${profile.gid}@asana.user`,
17641764
image: profile.photo?.image_128x128 || undefined,
@@ -1834,7 +1834,7 @@ export const auth = betterAuth({
18341834
logger.info('Slack credential identifier', { teamId, userId, uniqueId, teamName })
18351835

18361836
return {
1837-
id: uniqueId,
1837+
id: `${uniqueId}-${crypto.randomUUID()}`,
18381838
name: teamName,
18391839
email: `${teamId}-${userId}@slack.bot`,
18401840
emailVerified: false,
@@ -1884,7 +1884,7 @@ export const auth = betterAuth({
18841884
const uniqueId = `webflow-${userId}`
18851885

18861886
return {
1887-
id: uniqueId,
1887+
id: `${uniqueId}-${crypto.randomUUID()}`,
18881888
name: data.user_name || 'Webflow User',
18891889
email: `${uniqueId.replace(/[^a-zA-Z0-9]/g, '')}@webflow.user`,
18901890
emailVerified: false,
@@ -1931,7 +1931,7 @@ export const auth = betterAuth({
19311931
const profile = await response.json()
19321932

19331933
return {
1934-
id: profile.sub,
1934+
id: `${profile.sub}-${crypto.randomUUID()}`,
19351935
name: profile.name || 'LinkedIn User',
19361936
email: profile.email || `${profile.sub}@linkedin.user`,
19371937
emailVerified: profile.email_verified || true,
@@ -1993,7 +1993,7 @@ export const auth = betterAuth({
19931993
const profile = await response.json()
19941994

19951995
return {
1996-
id: profile.id,
1996+
id: `${profile.id}-${crypto.randomUUID()}`,
19971997
name:
19981998
`${profile.first_name || ''} ${profile.last_name || ''}`.trim() || 'Zoom User',
19991999
email: profile.email || `${profile.id}@zoom.user`,
@@ -2060,7 +2060,7 @@ export const auth = betterAuth({
20602060
const profile = await response.json()
20612061

20622062
return {
2063-
id: profile.id,
2063+
id: `${profile.id}-${crypto.randomUUID()}`,
20642064
name: profile.display_name || 'Spotify User',
20652065
email: profile.email || `${profile.id}@spotify.user`,
20662066
emailVerified: true,
@@ -2108,7 +2108,7 @@ export const auth = betterAuth({
21082108
const profile = await response.json()
21092109

21102110
return {
2111-
id: profile.ID?.toString() || profile.id?.toString(),
2111+
id: `${profile.ID?.toString() || profile.id?.toString()}-${crypto.randomUUID()}`,
21122112
name: profile.display_name || profile.username || 'WordPress User',
21132113
email: profile.email || `${profile.username}@wordpress.com`,
21142114
emailVerified: profile.email_verified || false,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DROP INDEX "account_user_provider_account_unique";--> statement-breakpoint
2+
CREATE UNIQUE INDEX "account_user_provider_unique" ON "account" USING btree ("user_id","provider_id");

0 commit comments

Comments
 (0)