src/auth/providers/set-cookie.provider.ts
Provider for setting cookies in HTTP responses. Designed to be used per HTTP request.
Methods |
|
constructor(jwtConfiguration: ConfigType<>)
|
||||||||
|
Defined in src/auth/providers/set-cookie.provider.ts:14
|
||||||||
|
Injects the JWT configuration and the current HTTP request object.
Parameters :
|
| Public setAccessToken | ||||||||||||
setAccessToken(accessToken: string, res: Response)
|
||||||||||||
|
Defined in src/auth/providers/set-cookie.provider.ts:44
|
||||||||||||
|
Sets the access token as a cookie in the HTTP response.
Parameters :
Returns :
void
|
| Public setRefreshToken | ||||||||||||
setRefreshToken(refreshToken: string, res: Response)
|
||||||||||||
|
Defined in src/auth/providers/set-cookie.provider.ts:29
|
||||||||||||
|
Sets the access token as a cookie in the HTTP response.
Parameters :
Returns :
void
|
import { Inject, Injectable, Scope } from '@nestjs/common';
import { Response } from 'express';
import jwtConfig from '../config/jwt.config';
import { ConfigType } from '@nestjs/config';
import { ACCESS_TOKEN_COOKIE_NAME, REFRESH_TOKEN_COOKIE_NAME } from '../constants/auth.constants';
/**
* Provider for setting cookies in HTTP responses.
* Designed to be used per HTTP request.
* @class SetCookieProvider
* @version 1
*/
@Injectable({ scope: Scope.REQUEST })
export class SetCookieProvider {
/**
* Injects the JWT configuration and the current HTTP request object.
* @param jwtConfiguration The JWT configuration containing token settings.
*/
constructor(
@Inject(jwtConfig.KEY)
private readonly jwtConfiguration: ConfigType<typeof jwtConfig>,
) {}
/**
* Sets the access token as a cookie in the HTTP response.
* @param accessToken The access token to be set as a cookie.
* @param res The HTTP response object.
*/
public setRefreshToken(refreshToken: string, res: Response) {
res.cookie(REFRESH_TOKEN_COOKIE_NAME, refreshToken, {
httpOnly: true,
secure: false /* process.env.NODE_ENV === 'production' */,
sameSite: 'lax',
path: '/api/v1/auth/refresh-tokens',
maxAge: this.jwtConfiguration.refreshTokenTtl * 1000, // Convert seconds to milliseconds
});
}
/**
* Sets the access token as a cookie in the HTTP response.
* @param accessToken The access token to be set as a cookie.
* @param res The HTTP response object.
*/
public setAccessToken(accessToken: string, res: Response) {
res.cookie(ACCESS_TOKEN_COOKIE_NAME, accessToken, {
httpOnly: true,
secure: false /* process.env.NODE_ENV === 'production' */,
sameSite: 'lax',
maxAge: this.jwtConfiguration.refreshTokenTtl * 1000, // Convert seconds to milliseconds
});
}
}