Decorators
Decorator list
| Decorator | Returns | Description |
|---|---|---|
@AuthorizationToken() | string | undefined | Reads Authorization: Bearer <token> and returns the token. |
@TokenPayload() | TokenPayloadModel | undefined | Returns the decoded payload that AuthGuard attaches to request.tokenPayload. |
The module exports two decorators to make it easy to access the Bearer token and decoded payload in controllers.
@AuthorizationToken()
Reads the Authorization header and returns the token when the type is Bearer.
Example
example.controller.ts
import { Controller, Get, UseGuards } from '@nestjs/common';
import { AuthGuard, AuthorizationToken } from 'nestjs-auth-module';
@Controller('example')
export class ExampleController {
@Get('token')
@UseGuards(AuthGuard)
token(@AuthorizationToken() token: string) {
return { token };
}
}
@TokenPayload()
Returns the decoded payload that AuthGuard places on request.tokenPayload.
Example
example.controller.ts
import { Controller, Get, UseGuards } from '@nestjs/common';
import { AuthGuard, TokenPayload } from 'nestjs-auth-module';
@Controller('example')
export class ExampleController {
@Get('payload')
@UseGuards(AuthGuard)
payload(@TokenPayload() payload: Record<string, unknown>) {
return { payload };
}
}
note
@TokenPayload() depends on AuthGuard (or another mechanism) to populate request.tokenPayload.
If you don't apply the guard, the decorator may return undefined.