Skip to content

Commit 77f2f38

Browse files
committed
chore: improve API key management
- Added a query parameter to filter API keys by status (active, revoked, all) in the getApiKey endpoint. - Updated the AuthService to handle status filtering logic for API key retrieval. - Modified the frontend to support status-based API key listing and added a button to view revoked keys.
1 parent 5467a85 commit 77f2f38

File tree

4 files changed

+223
-77
lines changed

4 files changed

+223
-77
lines changed

api/src/auth/auth.controller.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Param,
99
Patch,
1010
Post,
11+
Query,
1112
Request,
1213
UseGuards,
1314
} from '@nestjs/common'
@@ -98,10 +99,20 @@ export class AuthController {
9899

99100
@UseGuards(AuthGuard)
100101
@ApiOperation({ summary: 'Get Api Key List (masked***)' })
102+
@ApiQuery({
103+
name: 'status',
104+
required: false,
105+
enum: ['active', 'revoked', 'all'],
106+
description:
107+
'Filter keys: active (default), revoked only, or all (legacy full list)',
108+
})
101109
@ApiBearerAuth()
102110
@Get('/api-keys')
103-
async getApiKey(@Request() req) {
104-
const data = await this.authService.getUserApiKeys(req.user)
111+
async getApiKey(
112+
@Request() req,
113+
@Query('status') status?: string,
114+
) {
115+
const data = await this.authService.getUserApiKeys(req.user, status)
105116
return { data }
106117
}
107118

api/src/auth/auth.service.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,36 @@ export class AuthService {
363363
return { apiKey, message: 'Save this key, it wont be shown again ;)' }
364364
}
365365

366-
async getUserApiKeys(currentUser: User) {
367-
return this.apiKeyModel.find({ user: currentUser._id }, null, {
366+
async getUserApiKeys(
367+
currentUser: User,
368+
statusParam?: string,
369+
) {
370+
const normalized =
371+
statusParam === undefined || statusParam === '' ? 'active' : statusParam
372+
if (!['active', 'revoked', 'all'].includes(normalized)) {
373+
throw new HttpException(
374+
{ error: 'Invalid status. Use active, revoked, or all.' },
375+
HttpStatus.BAD_REQUEST,
376+
)
377+
}
378+
const status = normalized as 'active' | 'revoked' | 'all'
379+
380+
const base = { user: currentUser._id }
381+
let filter: Record<string, unknown> = { ...base }
382+
383+
if (status === 'active') {
384+
filter = {
385+
...base,
386+
$or: [{ revokedAt: { $exists: false } }, { revokedAt: null }],
387+
}
388+
} else if (status === 'revoked') {
389+
filter = {
390+
...base,
391+
revokedAt: { $exists: true, $ne: null },
392+
}
393+
}
394+
395+
return this.apiKeyModel.find(filter, null, {
368396
sort: { createdAt: -1 },
369397
})
370398
}
@@ -387,9 +415,9 @@ export class AuthService {
387415
HttpStatus.NOT_FOUND,
388416
)
389417
}
390-
if (apiKey.usageCount > 0) {
418+
if (!apiKey.revokedAt) {
391419
throw new HttpException(
392-
{ error: 'Api key cannot be deleted' },
420+
{ error: 'Revoke this API key before you can delete it' },
393421
HttpStatus.BAD_REQUEST,
394422
)
395423
}

0 commit comments

Comments
 (0)