Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] provided an option to get cookie for ssr #171

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions projects/ngx-cookie-service/src/lib/cookie.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// not use `DOCUMENT` injection and therefore doesn't work well with AoT production builds.
// Package: https://github.com/BCJTI/ng2-cookies

import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
import { Inject, Injectable, PLATFORM_ID, InjectionToken, Optional } from '@angular/core';
import { DOCUMENT, isPlatformBrowser } from '@angular/common';
export const COOKIES: InjectionToken<String> = new InjectionToken<String>('COOKIES');

@Injectable({
providedIn: 'root',
Expand All @@ -13,6 +14,7 @@ export class CookieService {

constructor(
@Inject(DOCUMENT) private document: any,
@Optional() @Inject(COOKIES) private cookie: any,
// Get the `PLATFORM_ID` so we can check if we're in a browser.
@Inject(PLATFORM_ID) private platformId
) {
Expand Down Expand Up @@ -62,11 +64,16 @@ export class CookieService {
* @returns property value
*/
get(name: string): string {
if (this.documentIsAccessible && this.check(name)) {
let cookie = this.cookie
if (this.documentIsAccessible) {
cookie = this.document.cookie
}

if (cookie && this.check(name)) {
name = encodeURIComponent(name);

const regExp: RegExp = CookieService.getCookieRegExp(name);
const result: RegExpExecArray = regExp.exec(this.document.cookie);
const result: RegExpExecArray = regExp.exec(cookie);

return result[1] ? CookieService.safeDecodeURIComponent(result[1]) : '';
} else {
Expand All @@ -80,15 +87,19 @@ export class CookieService {
* @returns all the cookies in json
*/
getAll(): { [key: string]: string } {
if (!this.documentIsAccessible) {
let cookie = this.cookie
if (this.documentIsAccessible) {
cookie = this.document.cookie
}

if (!cookie) {
return {};
}

const cookies: { [key: string]: string } = {};
const document: any = this.document;

if (document.cookie && document.cookie !== '') {
document.cookie.split(';').forEach((currentCookie) => {
if (cookie && cookie !== '') {
cookie.split(';').forEach((currentCookie) => {
const [cookieName, cookieValue] = currentCookie.split('=');
cookies[CookieService.safeDecodeURIComponent(cookieName.replace(/^ /, ''))] = CookieService.safeDecodeURIComponent(cookieValue);
});
Expand Down