Skip to main content

CookieTokenInterceptor

Package: @nauth-toolkit/nestjs Type: NestJS Interceptor

Intercepts responses containing AuthResponseDTO and sets authentication cookies based on delivery mode.

Import

import { CookieTokenInterceptor } from '@nauth-toolkit/nestjs';

Usage

Global Registration

import { Module } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { CookieTokenInterceptor } from '@nauth-toolkit/nestjs';

@Module({
providers: [
{
provide: APP_INTERCEPTOR,
useClass: CookieTokenInterceptor,
},
],
})
export class AppModule {}

Controller-Level

import { Controller, UseInterceptors } from '@nestjs/common';
import { CookieTokenInterceptor } from '@nauth-toolkit/nestjs';

@Controller('auth')
@UseInterceptors(CookieTokenInterceptor)
export class AuthController {}

Behavior

  • Detects AuthResponseDTO in response body
  • Sets accessToken and refreshToken cookies based on delivery mode
  • Removes tokens from JSON body when using cookie delivery
  • Sets CSRF token cookie

Configured via config.tokenDelivery.cookieOptions:

OptionTypeDescription
securebooleanHTTPS only
httpOnlybooleanAlways true --- hardcoded for security, cannot be disabled
sameSite'strict' | 'lax' | 'none'CSRF protection
domainstringCookie domain
pathstringCookie path (defaults to '/')
maxAgenumberCookie max age in milliseconds