[ADF-4897] delete test-cookie after check (#5164)

* remove cookie test after check

* remove cookie test after check

* remove cookie test after check

* remove cookie test after check

* make nullable variable optional in cookie

* make nullable variable optional in cookie
This commit is contained in:
Eugenio Romano 2019-10-17 00:16:11 +01:00 committed by GitHub
parent 313e535b8d
commit 48aca2d30f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,18 +22,25 @@ import { Injectable } from '@angular/core';
}) })
export class CookieService { export class CookieService {
cookieEnabled = false;
constructor() {
// for certain scenarios Chrome may say 'true' but have cookies still disabled
if (navigator.cookieEnabled === false) {
this.cookieEnabled = false;
}
this.setItem('test-cookie', 'test');
this.cookieEnabled = document.cookie.indexOf('test-cookie') >= 0;
this.deleteCookie('test-cookie');
}
/** /**
* Checks if cookies are enabled. * Checks if cookies are enabled.
* @returns True if enabled, false otherwise * @returns True if enabled, false otherwise
*/ */
isEnabled(): boolean { isEnabled(): boolean {
// for certain scenarios Chrome may say 'true' but have cookies still disabled return this.cookieEnabled;
if (navigator.cookieEnabled === false) {
return false;
}
document.cookie = 'test-cookie';
return document.cookie.indexOf('test-cookie') >= 0;
} }
/** /**
@ -54,12 +61,20 @@ export class CookieService {
* @param expiration Expiration date of the data * @param expiration Expiration date of the data
* @param path "Pathname" to store the cookie * @param path "Pathname" to store the cookie
*/ */
setItem(key: string, data: string, expiration: Date | null, path: string | null): void { setItem(key: string, data: string, expiration: Date | null = null, path: string | null = null): void {
document.cookie = `${key}=${data}` + document.cookie = `${key}=${data}` +
(expiration ? ';expires=' + expiration.toUTCString() : '') + (expiration ? ';expires=' + expiration.toUTCString() : '') +
(path ? `;path=${path}` : ';path=/'); (path ? `;path=${path}` : ';path=/');
} }
/**
* Delete a cookie Key.
* @param key Key to identify the cookie
*/
deleteCookie(key: string): void {
document.cookie = key + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
/** Placeholder for testing purposes - do not use. */ /** Placeholder for testing purposes - do not use. */
clear() { clear() {
/* placeholder for testing purposes */ /* placeholder for testing purposes */