Skip to content

Commit 44f5d9e

Browse files
committed
fix: ts errors
1 parent 7422e2d commit 44f5d9e

19 files changed

+114
-51
lines changed

src/runtime/server/api/nuxt-users/inactive.get.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default defineEventHandler(async (event) => {
2727
try {
2828
// Get total count for pagination
2929
const countResult = await db.sql`SELECT COUNT(*) as total FROM {${usersTable}} WHERE active = FALSE` as { rows: Array<{ total: number }> }
30-
const total = countResult.rows[0].total
30+
const total = countResult.rows[0]?.total ?? 0
3131

3232
// Get users with pagination (excluding passwords)
3333
const usersResult = await db.sql`

src/runtime/server/api/nuxt-users/index.get.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default defineEventHandler(async (event) => {
2727
try {
2828
// Get total count for pagination
2929
const countResult = await db.sql`SELECT COUNT(*) as total FROM {${usersTable}} WHERE active = TRUE` as { rows: Array<{ total: number }> }
30-
const total = countResult.rows[0].total
30+
const total = countResult.rows[0]?.total ?? 0
3131

3232
// Get users with pagination (excluding passwords)
3333
const usersResult = await db.sql`

src/runtime/server/api/nuxt-users/session/index.post.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ export default defineEventHandler(async (event) => {
3232
}
3333

3434
const user = userResult.rows[0]
35+
if (!user) {
36+
throw createError({
37+
statusCode: 401,
38+
statusMessage: 'Invalid email or password',
39+
})
40+
}
41+
3542
if (!user.active) {
3643
throw createError({
3744
statusCode: 403,

src/runtime/server/services/password.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,19 @@ export const resetPassword = async (
155155

156156
// Parse the original timestamp and add expiration hours
157157
const [datePart, timePart] = validTokenRecord.created_at.split(/[ T]/) // Split on space or T
158+
if (!datePart || !timePart) {
159+
console.log(`[Nuxt Users] Invalid timestamp format for token: ${validTokenRecord.created_at}`)
160+
return false
161+
}
162+
158163
const [year, month, day] = datePart.split('-').map(Number)
159164
const [hour, minute, second] = timePart.split(':').map(Number)
160165

166+
if (!year || !month || !day || hour === undefined || minute === undefined || second === undefined) {
167+
console.log(`[Nuxt Users] Invalid timestamp components`)
168+
return false
169+
}
170+
161171
// Calculate expiration time by adding hours
162172
let expirationHour = hour + TOKEN_EXPIRATION_HOURS
163173
let expirationDay = day

src/runtime/server/services/registration.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ export const registerUser = async (
6262
}
6363

6464
const user = result.rows[0]
65+
if (!user) {
66+
throw new Error('Failed to retrieve created user.')
67+
}
6568

6669
// Store confirmation token (reuse the password_reset_tokens table structure for confirmation tokens)
6770
const passwordResetTokensTable = options.tables.passwordResetTokens
@@ -226,9 +229,19 @@ export const confirmUserEmail = async (
226229
: String(validTokenRecord.created_at)
227230

228231
const [datePart, timePart] = createdAtString.split(/[ T]/)
232+
if (!datePart || !timePart) {
233+
console.log(`[Nuxt Users] Invalid timestamp format for token: ${createdAtString}`)
234+
return false
235+
}
236+
229237
const [year, month, day] = datePart.split('-').map(Number)
230238
const [hour, minute, second] = timePart.split(':').map(Number)
231239

240+
if (!year || !month || !day || hour === undefined || minute === undefined || second === undefined) {
241+
console.log(`[Nuxt Users] Invalid timestamp components`)
242+
return false
243+
}
244+
232245
// Calculate expiration time by adding hours
233246
let expirationHour = hour + TOKEN_EXPIRATION_HOURS
234247
let expirationDay = day
@@ -244,7 +257,9 @@ export const confirmUserEmail = async (
244257
if (expirationYear % 4 === 0 && (expirationYear % 100 !== 0 || expirationYear % 400 === 0)) {
245258
daysInMonth[1] = 29 // leap year
246259
}
247-
if (expirationDay > daysInMonth[expirationMonth - 1]) {
260+
const monthIndex = expirationMonth - 1
261+
const daysInCurrentMonth = daysInMonth[monthIndex]
262+
if (daysInCurrentMonth && expirationDay > daysInCurrentMonth) {
248263
expirationDay = 1
249264
expirationMonth++
250265
if (expirationMonth > 12) {

src/runtime/server/utils/google-oauth.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ export async function findOrCreateGoogleUser(
9797

9898
if (userResult.rows.length > 0) {
9999
const user = userResult.rows[0]
100+
if (!user) {
101+
return null
102+
}
100103

101104
// Update profile picture if it has changed
102105
if (user.profile_picture !== googleUser.picture) {
@@ -120,6 +123,10 @@ export async function findOrCreateGoogleUser(
120123
if (userResult.rows.length > 0) {
121124
// User exists with same email, link Google account
122125
const user = userResult.rows[0]
126+
if (!user) {
127+
return null
128+
}
129+
123130
await db.sql`
124131
UPDATE {${usersTable}}
125132
SET google_id = ${googleUser.id},
@@ -164,7 +171,7 @@ export async function findOrCreateGoogleUser(
164171
WHERE google_id = ${googleUser.id}
165172
` as { rows: User[] }
166173

167-
return createdUserResult.rows[0]
174+
return createdUserResult.rows[0] ?? null
168175
}
169176

170177
/**

src/runtime/server/utils/user.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

test/cli/create-migrations-table.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ describe('CLI: Create Migrations Table', () => {
128128
const migrations = result.rows || []
129129

130130
expect(migrations).toHaveLength(2)
131-
expect(migrations[0].id).toBe(1)
132-
expect(migrations[1].id).toBe(2)
131+
expect(migrations[0]?.id).toBe(1)
132+
expect(migrations[1]?.id).toBe(2)
133133
})
134134

135135
it('should set default timestamp', async () => {

test/cli/create-password-reset-tokens-table.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ describe('CLI: Create Password Reset Tokens Table', () => {
130130
const tokens = result.rows || []
131131

132132
expect(tokens).toHaveLength(2)
133-
expect(tokens[0].id).toBe(1)
134-
expect(tokens[1].id).toBe(2)
133+
expect(tokens[0]?.id).toBe(1)
134+
expect(tokens[1]?.id).toBe(2)
135135
})
136136

137137
it('should set default timestamp', async () => {

test/cli/create-personal-access-tokens-table.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ describe('CLI: Create Personal Access Tokens Table', () => {
135135
const tokens = result.rows || []
136136

137137
expect(tokens).toHaveLength(2)
138-
expect(tokens[0].id).toBe(1)
139-
expect(tokens[1].id).toBe(2)
138+
expect(tokens[0]?.id).toBe(1)
139+
expect(tokens[1]?.id).toBe(2)
140140
})
141141

142142
it('should set default timestamps', async () => {

0 commit comments

Comments
 (0)