@@ -41,41 +41,43 @@ export async function upsertOAuthAccount(
4141 } )
4242
4343 if ( ! user ) {
44- console . error ( `[AUTH:ERROR] OAuth account exists for ${ provider } :${ profile . id } but user ${ existingAccount . userId } not found` )
44+ console . error (
45+ `[AUTH:ERROR] OAuth account exists for ${ provider } :${ profile . id } but user ${ existingAccount . userId } not found` ,
46+ )
4547 throw new Error ( 'User not found for existing OAuth account' )
4648 }
4749
4850 if ( user ) {
49- const updates : {
50- email ?: string
51- name ?: string
52- image ?: string
53- } = { }
54-
55- if ( profile . email && user . email !== profile . email ) {
56- updates . email = profile . email
57- }
58- if ( profile . name && user . name !== profile . name ) {
59- updates . name = profile . name
60- }
61- if ( profile . image && user . image !== profile . image ) {
62- updates . image = profile . image
51+ const updates : {
52+ email ?: string
53+ name ?: string
54+ image ?: string
55+ } = { }
56+
57+ if ( profile . email && user . email !== profile . email ) {
58+ updates . email = profile . email
59+ }
60+ if ( profile . name && user . name !== profile . name ) {
61+ updates . name = profile . name
62+ }
63+ if ( profile . image && user . image !== profile . image ) {
64+ updates . image = profile . image
65+ }
66+
67+ if ( Object . keys ( updates ) . length > 0 ) {
68+ await db
69+ . update ( users )
70+ . set ( { ...updates , updatedAt : new Date ( ) } )
71+ . where ( eq ( users . id , existingAccount . userId ) )
72+ }
6373 }
6474
65- if ( Object . keys ( updates ) . length > 0 ) {
66- await db
67- . update ( users )
68- . set ( { ...updates , updatedAt : new Date ( ) } )
69- . where ( eq ( users . id , existingAccount . userId ) )
75+ return {
76+ userId : existingAccount . userId ,
77+ isNewUser : false ,
7078 }
7179 }
7280
73- return {
74- userId : existingAccount . userId ,
75- isNewUser : false ,
76- }
77- }
78-
7981 // Find user by email (for linking multiple OAuth providers)
8082 const existingUser = await db . query . users . findFirst ( {
8183 where : eq ( users . email , profile . email ) ,
@@ -85,7 +87,9 @@ export async function upsertOAuthAccount(
8587
8688 if ( existingUser ) {
8789 // Link OAuth account to existing user
88- console . log ( `[AUTH:INFO] Linking ${ provider } account to existing user ${ existingUser . id } (${ profile . email } )` )
90+ console . log (
91+ `[AUTH:INFO] Linking ${ provider } account to existing user ${ existingUser . id } (${ profile . email } )` ,
92+ )
8993 userId = existingUser . id
9094
9195 // Update user info if provided
@@ -109,7 +113,9 @@ export async function upsertOAuthAccount(
109113 }
110114 } else {
111115 // Create new user
112- console . log ( `[AUTH:INFO] Creating new user for ${ provider } login: ${ profile . email } ` )
116+ console . log (
117+ `[AUTH:INFO] Creating new user for ${ provider } login: ${ profile . email } ` ,
118+ )
113119 const [ newUser ] = await db
114120 . insert ( users )
115121 . values ( {
@@ -122,7 +128,9 @@ export async function upsertOAuthAccount(
122128 . returning ( )
123129
124130 if ( ! newUser ) {
125- console . error ( `[AUTH:ERROR] Failed to create user for ${ provider } :${ profile . id } (${ profile . email } )` )
131+ console . error (
132+ `[AUTH:ERROR] Failed to create user for ${ provider } :${ profile . id } (${ profile . email } )` ,
133+ )
126134 throw new Error ( 'Failed to create user' )
127135 }
128136
@@ -144,10 +152,13 @@ export async function upsertOAuthAccount(
144152 isNewUser : ! existingUser ,
145153 }
146154 } catch ( error ) {
147- console . error ( `[AUTH:ERROR] Failed to upsert OAuth account for ${ provider } :${ profile . id } (${ profile . email } ):` , {
148- error : error instanceof Error ? error . message : 'Unknown error' ,
149- stack : error instanceof Error ? error . stack : undefined ,
150- } )
155+ console . error (
156+ `[AUTH:ERROR] Failed to upsert OAuth account for ${ provider } :${ profile . id } (${ profile . email } ):` ,
157+ {
158+ error : error instanceof Error ? error . message : 'Unknown error' ,
159+ stack : error instanceof Error ? error . stack : undefined ,
160+ } ,
161+ )
151162 throw error
152163 }
153164}
0 commit comments