Skip to content

Commit 5453da9

Browse files
committed
fix: auto whitelist /me endpoint
1 parent e6cfbfb commit 5453da9

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

scripts/test-integration.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ sleep 2
7272

7373
print_status $BLUE "🧪 Running integration tests..."
7474

75-
# Test 1: Protected endpoint should return 401
76-
print_status $YELLOW "🔐 Testing protected endpoint /api/nuxt-users/me..."
75+
# Test 1: /me endpoint should require authentication (401 without token)
76+
print_status $YELLOW "🔐 Testing /me endpoint requires authentication..."
7777
response=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:3000/api/nuxt-users/me" || echo "000")
7878

7979
if [ "$response" = "401" ]; then
80-
print_status $GREEN "Middleware working - protected endpoint returns 401"
80+
print_status $GREEN "/me endpoint properly requires authentication (401 without token)"
8181
else
82-
print_status $RED "Middleware NOT working - expected 401, got $response"
82+
print_status $RED "/me endpoint should require authentication - expected 401, got $response"
8383
print_status $RED " This means server middleware is not properly registered!"
8484
exit 1
8585
fi

src/runtime/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
export const NO_AUTH_PATHS = ['/login', '/reset-password']
33

44
// API routes that don't require authentication (will be prefixed with apiBasePath)
5-
export const NO_AUTH_API_PATHS = ['/session', '/password/forgot', '/password/reset', '/me']
5+
export const NO_AUTH_API_PATHS = ['/session', '/password/forgot', '/password/reset']

src/runtime/server/middleware/authorization.server.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ export default defineEventHandler(async (event) => {
6969
}
7070
}
7171

72+
// Auto-whitelist /me endpoint for any authenticated user
73+
if (event.path === `${base}/me`) {
74+
console.log(`[Nuxt Users] server.middleware.auth.global: Auto-whitelisted /me endpoint for authenticated user ${user.id}`)
75+
return
76+
}
77+
7278
// Check role-based permissions
7379
if (!hasPermission(user.role, event.path, event.method, options.auth.permissions)) {
7480
if (event.path.startsWith('/api/')) {

test/unit/authorization.server.middleware.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,5 +473,32 @@ describe('Auth Server Middleware', () => {
473473
expect(mockCreateError).not.toHaveBeenCalled()
474474
expect(result).toBeUndefined()
475475
})
476+
477+
it('should auto-whitelist /me endpoint for any authenticated user', async () => {
478+
const event = { path: '/api/nuxt-users/me', method: 'GET' } as H3Event
479+
480+
// Mock getCookie to return a valid token
481+
mockGetCookie.mockReturnValue('valid-token')
482+
483+
// Mock getCurrentUserFromToken to return a user with unknown role (not in permissions)
484+
const { getCurrentUserFromToken } = await import('../../src/runtime/server/utils')
485+
const mockUser = {
486+
id: 5,
487+
email: 'unknown@example.com',
488+
name: 'Unknown Role User',
489+
role: 'unknown', // This role is not in permissions but /me should still work
490+
created_at: '2024-01-01T00:00:00Z',
491+
updated_at: '2024-01-01T00:00:00Z',
492+
active: true
493+
}
494+
vi.mocked(getCurrentUserFromToken).mockResolvedValue(mockUser)
495+
496+
const result = await serverAuthMiddleware.default(event)
497+
498+
expect(mockGetCookie).toHaveBeenCalledWith(event, 'auth_token')
499+
expect(getCurrentUserFromToken).toHaveBeenCalledWith('valid-token', mockOptions)
500+
expect(mockCreateError).not.toHaveBeenCalled()
501+
expect(result).toBeUndefined()
502+
})
476503
})
477504
})

0 commit comments

Comments
 (0)