Skip to content

Commit 28a5c00

Browse files
committed
fix: register date issue with MySQL
1 parent 1129088 commit 28a5c00

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/runtime/server/services/registration.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ export const confirmUserEmail = async (
193193
SELECT * FROM {${passwordResetTokensTable}}
194194
WHERE email = ${email}
195195
ORDER BY created_at DESC
196-
` as { rows: { id: number, email: string, token: string, created_at: string }[] }
196+
` as { rows: { id: number, email: string, token: string, created_at: Date | string }[] }
197197

198198
if (tokenRecords.rows.length === 0) {
199199
console.log(`[Nuxt Users] No confirmation tokens found for email: ${email}`)
@@ -220,7 +220,12 @@ export const confirmUserEmail = async (
220220
const currentTimeString = now.toISOString().slice(0, 19).replace('T', ' ')
221221

222222
// Parse the original timestamp and add expiration hours
223-
const [datePart, timePart] = validTokenRecord.created_at.split(/[ T]/)
223+
// Handle both Date objects and string timestamps from different databases
224+
const createdAtString = validTokenRecord.created_at instanceof Date
225+
? validTokenRecord.created_at.toISOString().slice(0, 19).replace('T', ' ')
226+
: String(validTokenRecord.created_at)
227+
228+
const [datePart, timePart] = createdAtString.split(/[ T]/)
224229
const [year, month, day] = datePart.split('-').map(Number)
225230
const [hour, minute, second] = timePart.split(':').map(Number)
226231

test/unit/api.register.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ describe('API: Registration', async () => {
5353
})).rejects.toThrow()
5454
})
5555

56-
it('should handle email confirmation', async () => {
57-
// This test would require mocking the token generation
58-
// For now, just test that the endpoint exists
56+
it('should handle email confirmation with invalid token', async () => {
57+
// Test that the endpoint properly validates tokens
5958
const uniqueEmail = `test-${Date.now()}-${Math.random().toString(36).substr(2, 9)}@example.com`
6059
await expect($fetch('/api/nuxt-users/confirm-email', {
6160
query: {
@@ -64,4 +63,13 @@ describe('API: Registration', async () => {
6463
}
6564
})).rejects.toThrow('Invalid or expired')
6665
})
66+
67+
it('should handle email confirmation with missing parameters', async () => {
68+
// Test parameter validation
69+
await expect($fetch('/api/nuxt-users/confirm-email', {
70+
query: {
71+
// Missing both token and email
72+
}
73+
})).rejects.toThrow('Token and email are required')
74+
})
6775
})

0 commit comments

Comments
 (0)