208 lines
25 KiB
JavaScript
208 lines
25 KiB
JavaScript
|
// This service is based on the `ng2-cookies` package which sadly is not a service and does
|
||
|
// 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 { DOCUMENT, isPlatformBrowser } from '@angular/common';
|
||
|
import * as i0 from "@angular/core";
|
||
|
export class CookieService {
|
||
|
constructor(document,
|
||
|
// Get the `PLATFORM_ID` so we can check if we're in a browser.
|
||
|
platformId) {
|
||
|
this.document = document;
|
||
|
this.platformId = platformId;
|
||
|
this.documentIsAccessible = isPlatformBrowser(this.platformId);
|
||
|
}
|
||
|
/**
|
||
|
* Get cookie Regular Expression
|
||
|
*
|
||
|
* @param name Cookie name
|
||
|
* @returns property RegExp
|
||
|
*
|
||
|
* @author: Stepan Suvorov
|
||
|
* @since: 1.0.0
|
||
|
*/
|
||
|
static getCookieRegExp(name) {
|
||
|
const escapedName = name.replace(/([\[\]{}()|=;+?,.*^$])/gi, '\\$1');
|
||
|
return new RegExp('(?:^' + escapedName + '|;\\s*' + escapedName + ')=(.*?)(?:;|$)', 'g');
|
||
|
}
|
||
|
/**
|
||
|
* Gets the unencoded version of an encoded component of a Uniform Resource Identifier (URI).
|
||
|
*
|
||
|
* @param encodedURIComponent A value representing an encoded URI component.
|
||
|
*
|
||
|
* @returns The unencoded version of an encoded component of a Uniform Resource Identifier (URI).
|
||
|
*
|
||
|
* @author: Stepan Suvorov
|
||
|
* @since: 1.0.0
|
||
|
*/
|
||
|
static safeDecodeURIComponent(encodedURIComponent) {
|
||
|
try {
|
||
|
return decodeURIComponent(encodedURIComponent);
|
||
|
}
|
||
|
catch {
|
||
|
// probably it is not uri encoded. return as is
|
||
|
return encodedURIComponent;
|
||
|
}
|
||
|
}
|
||
|
/**
|
||
|
* Return `true` if {@link Document} is accessible, otherwise return `false`
|
||
|
*
|
||
|
* @param name Cookie name
|
||
|
* @returns boolean - whether cookie with specified name exists
|
||
|
*
|
||
|
* @author: Stepan Suvorov
|
||
|
* @since: 1.0.0
|
||
|
*/
|
||
|
check(name) {
|
||
|
if (!this.documentIsAccessible) {
|
||
|
return false;
|
||
|
}
|
||
|
name = encodeURIComponent(name);
|
||
|
const regExp = CookieService.getCookieRegExp(name);
|
||
|
return regExp.test(this.document.cookie);
|
||
|
}
|
||
|
/**
|
||
|
* Get cookies by name
|
||
|
*
|
||
|
* @param name Cookie name
|
||
|
* @returns property value
|
||
|
*
|
||
|
* @author: Stepan Suvorov
|
||
|
* @since: 1.0.0
|
||
|
*/
|
||
|
get(name) {
|
||
|
if (this.documentIsAccessible && this.check(name)) {
|
||
|
name = encodeURIComponent(name);
|
||
|
const regExp = CookieService.getCookieRegExp(name);
|
||
|
const result = regExp.exec(this.document.cookie);
|
||
|
return result[1] ? CookieService.safeDecodeURIComponent(result[1]) : '';
|
||
|
}
|
||
|
else {
|
||
|
return '';
|
||
|
}
|
||
|
}
|
||
|
/**
|
||
|
* Get all cookies in JSON format
|
||
|
*
|
||
|
* @returns all the cookies in json
|
||
|
*
|
||
|
* @author: Stepan Suvorov
|
||
|
* @since: 1.0.0
|
||
|
*/
|
||
|
getAll() {
|
||
|
if (!this.documentIsAccessible) {
|
||
|
return {};
|
||
|
}
|
||
|
const cookies = {};
|
||
|
const document = this.document;
|
||
|
if (document.cookie && document.cookie !== '') {
|
||
|
document.cookie.split(';').forEach((currentCookie) => {
|
||
|
const [cookieName, cookieValue] = currentCookie.split('=');
|
||
|
cookies[CookieService.safeDecodeURIComponent(cookieName.replace(/^ /, ''))] = CookieService.safeDecodeURIComponent(cookieValue);
|
||
|
});
|
||
|
}
|
||
|
return cookies;
|
||
|
}
|
||
|
set(name, value, expiresOrOptions, path, domain, secure, sameSite) {
|
||
|
if (!this.documentIsAccessible) {
|
||
|
return;
|
||
|
}
|
||
|
if (typeof expiresOrOptions === 'number' || expiresOrOptions instanceof Date || path || domain || secure || sameSite) {
|
||
|
const optionsBody = {
|
||
|
expires: expiresOrOptions,
|
||
|
path,
|
||
|
domain,
|
||
|
secure,
|
||
|
sameSite: sameSite ? sameSite : 'Lax',
|
||
|
};
|
||
|
this.set(name, value, optionsBody);
|
||
|
return;
|
||
|
}
|
||
|
let cookieString = encodeURIComponent(name) + '=' + encodeURIComponent(value) + ';';
|
||
|
const options = expiresOrOptions ? expiresOrOptions : {};
|
||
|
if (options.expires) {
|
||
|
if (typeof options.expires === 'number') {
|
||
|
const dateExpires = new Date(new Date().getTime() + options.expires * 1000 * 60 * 60 * 24);
|
||
|
cookieString += 'expires=' + dateExpires.toUTCString() + ';';
|
||
|
}
|
||
|
else {
|
||
|
cookieString += 'expires=' + options.expires.toUTCString() + ';';
|
||
|
}
|
||
|
}
|
||
|
if (options.path) {
|
||
|
cookieString += 'path=' + options.path + ';';
|
||
|
}
|
||
|
if (options.domain) {
|
||
|
cookieString += 'domain=' + options.domain + ';';
|
||
|
}
|
||
|
if (options.secure === false && options.sameSite === 'None') {
|
||
|
options.secure = true;
|
||
|
console.warn(`[ngx-cookie-service] Cookie ${name} was forced with secure flag because sameSite=None.` +
|
||
|
`More details : https://github.com/stevermeister/ngx-cookie-service/issues/86#issuecomment-597720130`);
|
||
|
}
|
||
|
if (options.secure) {
|
||
|
cookieString += 'secure;';
|
||
|
}
|
||
|
if (!options.sameSite) {
|
||
|
options.sameSite = 'Lax';
|
||
|
}
|
||
|
cookieString += 'sameSite=' + options.sameSite + ';';
|
||
|
this.document.cookie = cookieString;
|
||
|
}
|
||
|
/**
|
||
|
* Delete cookie by name
|
||
|
*
|
||
|
* @param name Cookie name
|
||
|
* @param path Cookie path
|
||
|
* @param domain Cookie domain
|
||
|
* @param secure Cookie secure flag
|
||
|
* @param sameSite Cookie sameSite flag - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite
|
||
|
*
|
||
|
* @author: Stepan Suvorov
|
||
|
* @since: 1.0.0
|
||
|
*/
|
||
|
delete(name, path, domain, secure, sameSite = 'Lax') {
|
||
|
if (!this.documentIsAccessible) {
|
||
|
return;
|
||
|
}
|
||
|
const expiresDate = new Date('Thu, 01 Jan 1970 00:00:01 GMT');
|
||
|
this.set(name, '', { expires: expiresDate, path, domain, secure, sameSite });
|
||
|
}
|
||
|
/**
|
||
|
* Delete all cookies
|
||
|
*
|
||
|
* @param path Cookie path
|
||
|
* @param domain Cookie domain
|
||
|
* @param secure Is the Cookie secure
|
||
|
* @param sameSite Is the cookie same site
|
||
|
*
|
||
|
* @author: Stepan Suvorov
|
||
|
* @since: 1.0.0
|
||
|
*/
|
||
|
deleteAll(path, domain, secure, sameSite = 'Lax') {
|
||
|
if (!this.documentIsAccessible) {
|
||
|
return;
|
||
|
}
|
||
|
const cookies = this.getAll();
|
||
|
for (const cookieName in cookies) {
|
||
|
if (cookies.hasOwnProperty(cookieName)) {
|
||
|
this.delete(cookieName, path, domain, secure, sameSite);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.1", ngImport: i0, type: CookieService, deps: [{ token: DOCUMENT }, { token: PLATFORM_ID }], target: i0.ɵɵFactoryTarget.Injectable }); }
|
||
|
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.1", ngImport: i0, type: CookieService, providedIn: 'root' }); }
|
||
|
}
|
||
|
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.1", ngImport: i0, type: CookieService, decorators: [{
|
||
|
type: Injectable,
|
||
|
args: [{
|
||
|
providedIn: 'root',
|
||
|
}]
|
||
|
}], ctorParameters: function () { return [{ type: Document, decorators: [{
|
||
|
type: Inject,
|
||
|
args: [DOCUMENT]
|
||
|
}] }, { type: undefined, decorators: [{
|
||
|
type: Inject,
|
||
|
args: [PLATFORM_ID]
|
||
|
}] }]; } });
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uI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