返回 AiToEarn
aitoearn-auth.decorator.ts
根目录 / project / aitoearn-backend / libs / aitoearn-auth / src / aitoearn-auth.decorator.ts
1 import { createParamDecorator, ExecutionContext, SetMetadata } from '@nestjs/common'
2 import { API_KEY_HEADER_KEY, IS_INTERNAL_KEY, IS_PUBLIC_KEY } from './aitoearn-auth.constants'
3
4 export const GetToken = createParamDecorator(
5 (_data: string, ctx: ExecutionContext) => {
6 const req = ctx.switchToHttp().getRequest()
7 return req['user']
8 },
9 )
10
11 export const Public = () => SetMetadata(IS_PUBLIC_KEY, true)
12 export const Internal = () => SetMetadata(IS_INTERNAL_KEY, true)
13
14 /**
15 * 声明该路由额外支持从指定 header 读取 API Key 鉴权。
16 * @param headerName 额外兼容的 header 名,例如 'x-goog-api-key'、'Authorization'
17 */
18 export const ApiKeyHeader = (headerName: string) => SetMetadata(API_KEY_HEADER_KEY, headerName)
19
19 lines TYPESCRIPT