stevermeister / ngx-cookie-service

Angular (4.2+ ...12) service for cookies. Originally based on the `ng2-cookies` library.

Home Page:https://www.npmjs.com/package/ngx-cookie-service

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cookies dissapearing on page refresh and go back on next refresh

IgorKurkov opened this issue · comments

Expected Behavior

Should work properly with browser cookies

Actual Behavior

Cookies appear and disappear in each page refresh, without any idea. One injection of service, one "set cookie" function, it disappears, and on the next refresh, it appears, and again it disappears. No logic

Specifications

  • Version: 8.2.3
  • Browser: Chrome latest 87.0.4280.88

I know they're not so much information to research< I will try to create stackblitz. or can record video with dev-tools application tab. Only a thing - when I create a tiny service with my own methods to get/set cookies - they work as should. have no idea why ngx-cookie-service had this behavior.

Same behavior for me.

figured out this case by writing my own tiny cookieService. Hope it would be helpful while the issue willn't become resolved

import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class CookieService {
  constructor() {}

  set(name: string, value: string, days: number): void {
    let expires = '';
    if (days) {
      const date = new Date();
      date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
      expires = '; expires=' + date.toUTCString();
    }
    document.cookie = name + '=' + (value || '') + expires + '; path=/';
  }

  get(name: string): string | null {
    const nameEQ = name + '=';
    const ca = document.cookie.split(';');
    // tslint:disable-next-line:prefer-for-of
    for (let i = 0; i < ca.length; i++) {
      let c = ca[i];
      // tslint:disable-next-line:triple-equals
      while (c.charAt(0) == ' ') c = c.substring(1, c.length);
      // tslint:disable-next-line:triple-equals
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
  }

  delete(name: string): void {
    document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
  }
}

@IgorKurkov could you please create a sandbox for it?