fix: update email verification endpoint and improve error handling in apiFetch

This commit is contained in:
LaurensBartelds committed 2026-05-02 20:58:31 +02:00
1 parent 7849c295ef
commit 25e590639a
4 files changed
+28 -6

No files matched your search

+3 -2
View File
@@ -1,6 +1,7 @@
import { Router } from 'express' import { Router } from 'express'
import rateLimit from 'express-rate-limit' import rateLimit from 'express-rate-limit'
import { validateBody } from '../middleware/validate.js' import { validateBody } from '../middleware/validate.js'
import { validateQuery } from '../middleware/validate.js'
import { requireAuth } from '../middleware/auth.js' import { requireAuth } from '../middleware/auth.js'
import { asyncHandler } from '../utils/asyncHandler.js' import { asyncHandler } from '../utils/asyncHandler.js'
import * as authService from '../services/authService.js' import * as authService from '../services/authService.js'
@@ -86,9 +87,9 @@ router.post(
// GET /api/auth/verify // GET /api/auth/verify
router.get( router.get(
'/verify', '/verify',
validateBody(verifyEmailSchema), validateQuery(verifyEmailSchema),
asyncHandler(async (req, res) => { asyncHandler(async (req, res) => {
await authService.verifyEmail(req.body) await authService.verifyEmail(req.query as { token: string })
res.json({ message: 'Email verified successfully' }) res.json({ message: 'Email verified successfully' })
}) })
) )
+1 -1
View File
@@ -38,7 +38,7 @@ export function changePassword(data: { currentPassword: string; newPassword: str
} }
export function verifyEmailToken(token: string): Promise<{ message: string }> { export function verifyEmailToken(token: string): Promise<{ message: string }> {
return apiFetch(`/api/auth/verify-email?token=${encodeURIComponent(token)}`) return apiFetch(`/api/auth/verify?token=${encodeURIComponent(token)}`)
} }
export function forgotPassword(email?: string): Promise<{ message: string }> { export function forgotPassword(email?: string): Promise<{ message: string }> {
+22 -2
View File
@@ -1,5 +1,9 @@
import { describe, it, expect } from 'vitest' import { describe, it, expect, vi, afterEach } from 'vitest'
import { ApiError } from './api' import { apiFetch, ApiError } from './api'
afterEach(() => {
vi.restoreAllMocks()
})
describe('ApiError', () => { describe('ApiError', () => {
it('extends Error', () => { it('extends Error', () => {
@@ -18,3 +22,19 @@ describe('ApiError', () => {
expect(err.name).toBe('ApiError') expect(err.name).toBe('ApiError')
}) })
}) })
describe('apiFetch', () => {
it('uses message from error responses', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
ok: false,
status: 403,
statusText: 'Forbidden',
json: vi.fn().mockResolvedValue({ message: 'Please verify your email before logging in' }),
}))
await expect(apiFetch('/api/auth/login')).rejects.toMatchObject({
status: 403,
message: 'Please verify your email before logging in',
})
})
})
+2 -1
View File
@@ -40,7 +40,8 @@ export async function apiFetch<T = unknown>(
if (!res.ok) { if (!res.ok) {
const body = await res.json().catch(() => ({ error: res.statusText })) const body = await res.json().catch(() => ({ error: res.statusText }))
throw new ApiError(res.status, (body as { error?: string }).error ?? 'Request failed') const parsedBody = body as { error?: string; message?: string }
throw new ApiError(res.status, parsedBody.error ?? parsedBody.message ?? 'Request failed')
} }
return res.json() as Promise<T> return res.json() as Promise<T>