Skip to content

Commit c7524b4

Browse files
authored
Merge pull request #362 from stevermeister/development
feat(ssr-cookie): implement improved cookie retrieval from request
2 parents 99c84bf + 74f2492 commit c7524b4

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

projects/ngx-cookie-service-ssr/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ngx-cookie-service-ssr",
33
"description": "Angular SSR cookie service",
4-
"version": "20.0.0",
4+
"version": "20.1.0",
55
"license": "MIT",
66
"author": "Stepan Suvorov <[email protected]>",
77
"keywords": [

projects/ngx-cookie-service-ssr/src/lib/ssr-cookie.service.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isPlatformBrowser } from '@angular/common';
2-
import { inject, Injectable, PLATFORM_ID, REQUEST, DOCUMENT } from '@angular/core';
2+
import { DOCUMENT, inject, Injectable, PLATFORM_ID, REQUEST } from '@angular/core';
33
import { SameSite } from 'ngx-cookie-service';
44

55
@Injectable({
@@ -11,6 +11,35 @@ export class SsrCookieService {
1111
private readonly request = inject(REQUEST, { optional: true });
1212
private readonly documentIsAccessible: boolean = isPlatformBrowser(this.platformId);
1313

14+
/**
15+
* Helper method to safely get cookies from request object
16+
* Handles both Angular's REQUEST interface and Express's req interface
17+
*/
18+
private getRequestCookies(): string | null {
19+
if (!this.request) {
20+
return null;
21+
}
22+
23+
// Handle Angular REQUEST object (has headers.get method)
24+
if (this.request.headers && typeof this.request.headers.get === 'function') {
25+
return this.request.headers.get('cookie');
26+
}
27+
28+
// Handle Express request object (has headers object and get method)
29+
const reqAny = this.request as any;
30+
if (typeof reqAny.get === 'function') {
31+
return reqAny.get('cookie') || reqAny.get('Cookie');
32+
}
33+
34+
// Handle direct headers object access
35+
const headers = this.request.headers as any;
36+
if (headers && headers != null && typeof headers === 'object') {
37+
return headers['cookie'] || headers['Cookie'];
38+
}
39+
40+
return null;
41+
}
42+
1443
/**
1544
* Get cookie Regular Expression
1645
*
@@ -57,7 +86,7 @@ export class SsrCookieService {
5786
check(name: string): boolean {
5887
name = encodeURIComponent(name);
5988
const regExp: RegExp = SsrCookieService.getCookieRegExp(name);
60-
return regExp.test(this.documentIsAccessible ? this.document.cookie : this.request?.headers.get('cookie'));
89+
return regExp.test(this.documentIsAccessible ? this.document.cookie : this.getRequestCookies());
6190
}
6291

6392
/**
@@ -73,7 +102,7 @@ export class SsrCookieService {
73102
if (this.check(name)) {
74103
name = encodeURIComponent(name);
75104
const regExp: RegExp = SsrCookieService.getCookieRegExp(name);
76-
const result = regExp.exec(this.documentIsAccessible ? this.document.cookie : this.request?.headers.get('cookie'));
105+
const result = regExp.exec(this.documentIsAccessible ? this.document.cookie : this.getRequestCookies());
77106
return result?.[1] ? SsrCookieService.safeDecodeURIComponent(result[1]) : '';
78107
}
79108
return '';
@@ -89,7 +118,7 @@ export class SsrCookieService {
89118
*/
90119
getAll(): { [key: string]: string } {
91120
const cookies: { [key: string]: string } = {};
92-
const cookieString: any = this.documentIsAccessible ? this.document?.cookie : this.request?.headers.get('cookie');
121+
const cookieString: any = this.documentIsAccessible ? this.document?.cookie : this.getRequestCookies();
93122

94123
if (cookieString && cookieString !== '') {
95124
cookieString.split(';').forEach((currentCookie: string) => {

projects/ngx-cookie-service/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ngx-cookie-service",
33
"description": "Angular cookie service",
4-
"version": "20.0.0",
4+
"version": "20.1.0",
55
"license": "MIT",
66
"author": "Stepan Suvorov <[email protected]>",
77
"keywords": [

server-example.ts

Whitespace-only changes.

tsconfig.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,22 @@
77
"esModuleInterop": true,
88
"declaration": false,
99
"experimentalDecorators": true,
10-
"module": "es2020",
10+
"module": "ES2022",
1111
"moduleResolution": "bundler",
1212
"importHelpers": true,
1313
"target": "ES2022",
1414
"typeRoots": [
1515
"node_modules/@types"
1616
],
1717
"lib": [
18-
"es2020",
18+
"ES2022",
1919
"dom"
2020
],
2121
"paths": {
2222
"ngx-cookie-service": [
2323
"dist/ngx-cookie-service/ngx-cookie-service",
2424
"dist/ngx-cookie-service"
2525
],
26-
"ngx-cookie-service-ssr": [
27-
"dist/ngx-cookie-service-ssr"
28-
]
2926
},
3027
"useDefineForClassFields": false
3128
},

0 commit comments

Comments
 (0)