src/auth/api-keys.controller.ts
api-keys
Methods |
| Async create | ||||||||||||
create(orgId: string, userId: string, dto: CreateApiKeyDto)
|
||||||||||||
Decorators :
@Post()
|
||||||||||||
|
Defined in src/auth/api-keys.controller.ts:37
|
||||||||||||
|
Parameters :
Returns :
unknown
|
| Async list | ||||||
list(orgId: string)
|
||||||
Decorators :
@Get()
|
||||||
|
Defined in src/auth/api-keys.controller.ts:67
|
||||||
|
Parameters :
Returns :
unknown
|
| Async revoke |
revoke(id: string, orgId: string)
|
Decorators :
@Delete(':id')
|
|
Defined in src/auth/api-keys.controller.ts:75
|
|
Returns :
unknown
|
import {
Controller,
Post,
Get,
Delete,
Body,
Param,
UseGuards,
HttpCode,
HttpStatus,
} from '@nestjs/common';
import {
ApiTags,
ApiBearerAuth,
ApiOperation,
ApiResponse,
} from '@nestjs/swagger';
import { JwtAuthGuard } from './guards/jwt-auth.guard';
import { CurrentUser } from './decorators/current-user.decorator';
import { ApiKeysService } from './api-keys.service';
import { CreateApiKeyDto } from './dto/create-api-key.dto';
@ApiTags('API Keys')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('api-keys')
export class ApiKeysController {
constructor(private readonly apiKeysService: ApiKeysService) {}
@Post()
@ApiOperation({
summary: 'Create a new API key',
description:
'Generates a new API key for external integrations. The raw key is returned ONCE in the response and cannot be retrieved again.',
})
@ApiResponse({ status: 201, description: 'API key created. Raw key returned once.' })
async create(
@CurrentUser('organizationId') orgId: string,
@CurrentUser('id') userId: string,
@Body() dto: CreateApiKeyDto,
) {
const expiresAt = dto.expiresAt ? new Date(dto.expiresAt) : undefined;
const result = await this.apiKeysService.createKey(
orgId,
userId,
dto.name,
dto.permissions,
expiresAt,
);
return {
message:
'API key created. Save the key now — it will not be shown again.',
key: result.key,
id: result.record.id,
prefix: result.record.prefix,
name: result.record.name,
permissions: result.record.permissions,
};
}
@Get()
@ApiOperation({
summary: 'List all API keys',
description:
'Returns all API keys for the organization. Only the prefix is shown, not the full key.',
})
async list(@CurrentUser('organizationId') orgId: string) {
return this.apiKeysService.listKeys(orgId);
}
@Delete(':id')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Revoke an API key' })
@ApiResponse({ status: 200, description: 'API key revoked.' })
async revoke(
@Param('id') id: string,
@CurrentUser('organizationId') orgId: string,
) {
await this.apiKeysService.revokeKey(id, orgId);
return { message: 'API key revoked successfully' };
}
}