Skip to content

Commit

Permalink
use official request token that provided by angular
Browse files Browse the repository at this point in the history
  • Loading branch information
hizliemre committed Jan 26, 2025
1 parent 39339af commit 4096fe5
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions projects/ngx-cookie-service-ssr/src/lib/ssr-cookie.service.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import { Request } from 'express';
import { Inject, Injectable, InjectionToken, Optional, PLATFORM_ID } from '@angular/core';
import { Injectable, PLATFORM_ID, REQUEST, inject } from '@angular/core';
import { DOCUMENT, isPlatformBrowser } from '@angular/common';

// Define the `Request` token
export const REQUEST = new InjectionToken<Request>('REQUEST');

@Injectable({
providedIn: 'root',
})
export class SsrCookieService {
private readonly documentIsAccessible: boolean;

constructor(
@Inject(DOCUMENT) private document: Document,
// Get the `PLATFORM_ID` so we can check if we're in a browser.
@Inject(PLATFORM_ID) private platformId: any,
@Optional() @Inject(REQUEST) private request: Request
) {
this.documentIsAccessible = isPlatformBrowser(this.platformId);
}
private readonly document = inject(DOCUMENT);
// Get the `PLATFORM_ID` so we can check if we're in a browser.
private readonly platformId = inject(PLATFORM_ID);
private readonly request = inject(REQUEST, { optional: true });
private readonly documentIsAccessible: boolean = isPlatformBrowser(this.platformId);

/**
* Get cookie Regular Expression
Expand Down Expand Up @@ -66,7 +57,10 @@ export class SsrCookieService {
check(name: string): boolean {
name = encodeURIComponent(name);
const regExp: RegExp = SsrCookieService.getCookieRegExp(name);
return regExp.test(this.documentIsAccessible ? this.document.cookie : this.request?.headers.cookie);
const cookieString = this.documentIsAccessible
? this.document.cookie
: this.request?.headers.get('cookie');
return regExp.test(cookieString ?? '');
}

/**
Expand All @@ -83,9 +77,13 @@ export class SsrCookieService {
name = encodeURIComponent(name);

const regExp: RegExp = SsrCookieService.getCookieRegExp(name);
const result: RegExpExecArray = regExp.exec(this.documentIsAccessible ? this.document.cookie : this.request?.headers.cookie);
const cookieString = this.documentIsAccessible
? this.document.cookie
: this.request?.headers.get('cookie');
const result = regExp.exec(cookieString ?? '');
const value = result && result[1] ? result[1] : '';

return result[1] ? SsrCookieService.safeDecodeURIComponent(result[1]) : '';
return SsrCookieService.safeDecodeURIComponent(value);
} else {
return '';
}
Expand All @@ -101,7 +99,7 @@ export class SsrCookieService {
*/
getAll(): { [key: string]: string } {
const cookies: { [key: string]: string } = {};
const cookieString: any = this.documentIsAccessible ? this.document?.cookie : this.request?.headers.cookie;
const cookieString: any = this.documentIsAccessible ? this.document?.cookie : this.request?.headers.get('cookie');

if (cookieString && cookieString !== '') {
cookieString.split(';').forEach((currentCookie) => {
Expand Down

0 comments on commit 4096fe5

Please sign in to comment.