// Core imports import { Injectable } from '@angular/core'; // Ngx imports import { CookieService } from 'ngx-cookie'; // Shared app imports import { LanguageService } from '../router-language/route-language.service'; // Utility imports import { parse, format } from 'date-fns'; import * as frLocale from 'date-fns/locale/fr/index.js'; import * as enLocale from 'date-fns/locale/en/index.js'; @Injectable() export class UserInfoService { // This provides provides utility functions for getting user data for various purposes constructor( private cookieService: CookieService, private langService: LanguageService ) { } /* * Check if the cookie is sensative / private * In this case the check applied to `givenName`, `saveusername` and `email` * @param {string} coockieName - string cookie name * @return {boolean} - true or false base on matching the cookie name provided * */ isPrivateCookie = (cookieName: string) => ['givenName', 'saveusername', '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) { return 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 document !== 'undefined' ? this.cookieService.get(name) : null; 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 d = new Date(); const tzOffset = d.getTimezoneOffset(); const value = this.isPrivateCookie(cname) ? this.encodeCookie(cvalue) : cvalue; d.setTime(d.getTime() + (exdays * 86400000) + (tzOffset * -60000)); if (typeof document !== 'undefined') { document.cookie = `${cname}=${value}; expires=${d.toUTCString()}; domain=.homedepot.ca; path=/`; } } getLastLoginOn() { // Gets the date and time of the last login try { if (typeof document !== 'undefined') { window['__localeId__'] = this.langService.getLanguage(); let dateFormat = 'dddd, MMMM D, YYYY h:mm A'; let locale = enLocale; if (window['__localeId__'] === 'fr') { dateFormat = 'dddd D MMMM YYYY hh:mm'; locale = frLocale; } const LastLoginDateTime = parse(JSON.parse(this.cookieService.get('LastLoninTime'))); return format(LastLoginDateTime, dateFormat, {locale: locale}); } else { return null; } } catch (err) { return null; } } getUserAccountData() { // Retrieve user info const User = { savedEmailAddress: this.getCookie('saveusername'), givenName: this.getCookie('givenName', true), email: this.getCookie('email'), isAuth: !!this.getCookie('email'), isExpired: !!this.getCookie('expiredSession'), proxtraUserLoggedin: !!this.getCookie('proxtraUserLoggedin'), associateUserLoggedin: !!this.getCookie('associateUserLoggedin'), lastLoginOn: this.getLastLoginOn(), cartQuantity: this.getCookie('currentCartQuantity'), authorization: this.getCookie('uuid'), customer: this.getCookie('customer') }; return User; } clearUserExpiry() { // Clear the user expiry by setting expiry on email and expired session to -1 days this.setCookie('expiredSession', null, -1); this.setCookie('email', null, -1); } }