Skip to content

Commit dab5c43

Browse files
committed
fix(auth): refine cookie options for improved security and compatibility
- Updated cookie options to use the CookieOptions type from Express. - Ensured the domain attribute is set correctly for cross-site cookies. - Removed conditional domain setting to streamline cookie handling.
1 parent fdfb105 commit dab5c43

1 file changed

Lines changed: 6 additions & 22 deletions

File tree

apps/backend/src/auth/auth.service.ts

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Inject, Injectable, Logger } from '@nestjs/common';
22
import { JwtService } from '@nestjs/jwt';
33
import axios from 'axios';
4-
import type { Request, Response } from 'express';
4+
import type { CookieOptions, Request, Response } from 'express';
55
import ms from 'ms';
66

77
import { CreateUser } from '@nbw/database';
@@ -190,7 +190,7 @@ export class AuthService {
190190

191191
private async GenTokenRedirect(
192192
user_registered: UserDocument,
193-
res: Response<any, Record<string, any>>,
193+
res: Response<unknown, Record<string, unknown>>,
194194
): Promise<void> {
195195
const token = await this.createJwtPayload({
196196
id: user_registered._id.toString(),
@@ -201,29 +201,13 @@ export class AuthService {
201201
const frontEndURL = this.FRONTEND_URL;
202202
const maxAge = ms(this.COOKIE_EXPIRES_IN) * 1000;
203203

204-
// Build cookie options with conditional domain
205-
// Only include domain if it's set and not empty (avoids browser blocking invalid domains)
206-
const cookieOptions: {
207-
maxAge: number;
208-
sameSite: 'none' | 'lax' | 'strict';
209-
secure: boolean;
210-
httpOnly: boolean;
211-
path: string;
212-
domain?: string;
213-
} = {
204+
const cookieOptions: CookieOptions = {
214205
maxAge: maxAge,
215-
sameSite: 'none', // Required for cross-site cookies
216-
secure: true, // Required when sameSite is 'none'
217-
httpOnly: false, // Prevents JavaScript access (security best practice)
218-
path: '/', // Make cookies available site-wide
206+
domain: this.APP_DOMAIN,
207+
sameSite: 'none',
208+
path: '/',
219209
};
220210

221-
// Only set domain if APP_DOMAIN is provided and not empty
222-
// This prevents browser from blocking cookies with invalid domain attributes
223-
if (this.APP_DOMAIN && this.APP_DOMAIN.trim() !== '') {
224-
cookieOptions.domain = this.APP_DOMAIN;
225-
}
226-
227211
res.cookie('token', token.access_token, cookieOptions);
228212
res.cookie('refresh_token', token.refresh_token, cookieOptions);
229213

0 commit comments

Comments
 (0)