@@ -46,6 +46,9 @@ export const createUser = async (userData: CreateUserParams, options: ModuleOpti
4646 }
4747
4848 const user = result . rows [ 0 ]
49+ if ( ! user ) {
50+ throw new Error ( 'Failed to retrieve created user.' )
51+ }
4952
5053 // Convert Date objects to ISO strings if needed
5154 return {
@@ -73,6 +76,9 @@ export const findUserByEmail = async (email: string, options: ModuleOptions): Pr
7376 }
7477
7578 const user = result . rows [ 0 ]
79+ if ( ! user ) {
80+ return null
81+ }
7682
7783 // Convert Date objects to ISO strings if needed
7884 return {
@@ -105,6 +111,9 @@ export const findUserById = async <T extends boolean = false>(
105111 }
106112
107113 const user = result . rows [ 0 ]
114+ if ( ! user ) {
115+ return null
116+ }
108117
109118 const normalizedUser = {
110119 ...user ,
@@ -221,7 +230,8 @@ export const hasAnyUsers = async (options: ModuleOptions) => {
221230 const db = await useDb ( options )
222231
223232 const users = await db . sql `SELECT COUNT(*) as count FROM {${ options . tables . users } }` as CountResult
224- return users . rows ?. [ 0 ] ?. count > 0
233+ const firstRow = users . rows ?. [ 0 ]
234+ return firstRow ? firstRow . count > 0 : false
225235 }
226236 catch {
227237 // If the table doesn't exist or connection fails, there are no users
@@ -252,7 +262,12 @@ export const getCurrentUserFromToken = async <T extends boolean = false>(
252262 return null
253263 }
254264
255- const userId = tokenResult . rows [ 0 ] . tokenable_id
265+ const tokenRow = tokenResult . rows [ 0 ]
266+ if ( ! tokenRow ) {
267+ return null
268+ }
269+
270+ const userId = tokenRow . tokenable_id
256271
257272 // Update last_used_at timestamp for the token
258273 await db . sql `
@@ -272,6 +287,9 @@ export const getCurrentUserFromToken = async <T extends boolean = false>(
272287 }
273288
274289 const user = userResult . rows [ 0 ]
290+ if ( ! user ) {
291+ return null
292+ }
275293
276294 if ( ! user . active ) {
277295 return null
@@ -370,7 +388,12 @@ export const getLastLoginTime = async (userId: number, options: ModuleOptions):
370388 return null
371389 }
372390
373- const lastLogin = result . rows [ 0 ] . created_at
391+ const resultRow = result . rows [ 0 ]
392+ if ( ! resultRow ) {
393+ return null
394+ }
395+
396+ const lastLogin = resultRow . created_at
374397
375398 // Convert Date objects to ISO strings if needed
376399 return lastLogin instanceof Date ? lastLogin . toISOString ( ) : lastLogin
0 commit comments