import { Injectable, Inject } from '@angular/core'; import { CookieService } from 'ngx-cookie'; import { domain } from '../../tokens'; import * as constant from '../../constants/cookie.constants'; @Injectable() export class CookiesService { domain: string; constructor(private cookieService: CookieService, @Inject(domain) private shadowedDomain) { this.domain = shadowedDomain; } /* * Check if the cookie is sensative / private * In this case the check applied to `givenName`, `addressPostalCode`, `saveusername` and `email` * @param {string} coockieName - string cookie name * @return {boolean} - true or false base on matching the cookie name provided * */ isPrivateCookie = (cookieName: string) => [constant.userCookies.GIVEN_NAME, constant.userCookies.ADDRESS_POSTAL_CODE, constant.userCookies.SAVE_USER_NAME, constant.userCookies.EMAIL].indexOf(cookieName) > -1; /* * Simply reverse string * @param {string} str - string to reverse * @return {string} reversed string * */ reverse = (str: string) => str.split('').reverse().join(''); /* * Decoding the coockie from the encoded method for example `email` cookie * @param {string} cname - string cookie name * @return {string} - string decoded cookie name * */ decodeCookie = (cname: string) => { try { return decodeURIComponent(atob(this.reverse(cname)).split('').map(c => `%${(`00${c.charCodeAt(0).toString(16)}`).slice(-2)}`).join('')); } catch (err) { return cname; } } /* * Encoding the coockie * @param {string} cvalue - string cookie value * @return {string} - string encoded cookie value * */ encodeCookie = (cvalue: string) => this.reverse(btoa(encodeURIComponent(cvalue).replace(/%([0-9A-F]{2})/g, (match, p1) => String.fromCharCode.apply(null, [`0x${p1}`])))); /* * Get the coockie value based on cookie name * @param {string} name - string cookie name * @param {string} skipUnEscape - string cookie name * @return {string} - string decoded cookie name * */ getCookie(name: string, skipUnEscape?: any) { const cookie = typeof window !== 'undefined' ? this.cookieService.get(name) : ''; if (cookie) { const value = this.isPrivateCookie(name) ? this.decodeCookie(cookie) : cookie; return skipUnEscape ? value : decodeURIComponent(value); } return null; } /* * Set the coockie with the provide name, value and expiration * @param {string} cname - string cookie name * @param {string} cvalue - string cookie value * @param {string} exdays - expiration date * */ setCookie(cname: string, cvalue: string, exdays?: any) { const value = this.isPrivateCookie(cname) ? this.encodeCookie(cvalue) : cvalue; const expiryDate = new Date(); expiryDate.setDate(expiryDate.getDate() + exdays); typeof window !== 'undefined' && this.cookieService.put(cname, value, { expires: expiryDate, domain: this.domain, path: '/' }); } /* * Remove the cookie with the provided name * @param {string} key - string cookie name * */ removeCookie(key: string) { typeof window !== 'undefined' && this.cookieService.remove(key, { domain: this.domain, path: '/' }); } /* * Update currentCartQuantity cookie * Note: this is used only for iOS / Android native mobile app * @param {string} qty - total cart item quantity * */ updateCurrentCartQuantityCookie(qty: string): void { this.setCookie(constant.cartCookies.CURRENT_CART_QUANTITY, qty); } }