|
| 1 | +import { Inject, Injectable, Optional } from '@angular/core'; |
| 2 | +import { APP_BASE_HREF } from '@angular/common'; |
| 3 | + |
| 4 | +export interface CookieOptionsArgs { |
| 5 | + path?: string; |
| 6 | + domain?: string; |
| 7 | + expires?: string | Date; |
| 8 | + secure?: boolean; |
| 9 | +} |
| 10 | + |
| 11 | +export interface ICookies { |
| 12 | + [key: string]: string[]; |
| 13 | +} |
| 14 | + |
| 15 | +export interface ICookieService { |
| 16 | + get(key: string): string; |
| 17 | + set(key: string, value: string, options?: CookieOptionsArgs): void; |
| 18 | + remove(key: string, options?: CookieOptionsArgs): void; |
| 19 | +} |
| 20 | + |
| 21 | +export class CookieOptions { |
| 22 | + |
| 23 | + public path: string; |
| 24 | + public domain: string; |
| 25 | + public expires: string | Date; |
| 26 | + public secure: boolean; |
| 27 | + |
| 28 | + constructor({ path, domain, expires, secure }: CookieOptionsArgs = {}) { |
| 29 | + this.path = this.isPresent(path) ? path : null; |
| 30 | + this.domain = this.isPresent(domain) ? domain : null; |
| 31 | + this.expires = this.isPresent(expires) ? expires : null; |
| 32 | + this.secure = this.isPresent(secure) ? secure : false; |
| 33 | + } |
| 34 | + |
| 35 | + public merge(options?: CookieOptionsArgs): CookieOptions { |
| 36 | + return new CookieOptions(<CookieOptionsArgs>{ |
| 37 | + path: this.isPresent(options) && this.isPresent(options.path) ? options.path : this.path, |
| 38 | + domain: this.isPresent(options) && this.isPresent(options.domain) ? options.domain : this.domain, |
| 39 | + expires: this.isPresent(options) && this.isPresent(options.expires) ? options.expires : this.expires, |
| 40 | + secure: this.isPresent(options) && this.isPresent(options.secure) ? options.secure : this.secure, |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + private isPresent(obj: any): boolean { |
| 45 | + return obj !== undefined && obj !== null; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +@Injectable({ |
| 50 | + providedIn: 'root' |
| 51 | +}) |
| 52 | +export class BaseCookieOptions extends CookieOptions { |
| 53 | + constructor(@Optional() @Inject(APP_BASE_HREF) private baseHref: string) { |
| 54 | + super({ path: baseHref || '/' }); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +@Injectable({ |
| 59 | + providedIn: 'root' |
| 60 | +}) |
| 61 | +export class CookiesService implements ICookieService { |
| 62 | + |
| 63 | + constructor( |
| 64 | + @Optional() private defaultOptions?: CookieOptions |
| 65 | + ) { |
| 66 | + } |
| 67 | + |
| 68 | + protected get cookieString(): string { |
| 69 | + return document.cookie || ''; |
| 70 | + } |
| 71 | + |
| 72 | + protected set cookieString(val: string) { |
| 73 | + document.cookie = val; |
| 74 | + } |
| 75 | + |
| 76 | + private cookieReader(key: string): string { |
| 77 | + const currentCookieString = this.cookieString; |
| 78 | + |
| 79 | + if (currentCookieString) { |
| 80 | + const cookieArray = currentCookieString.split('; '); |
| 81 | + |
| 82 | + return cookieArray.reduce((cookies: ICookies, current: string) => { |
| 83 | + const cookie = current.split('='); |
| 84 | + |
| 85 | + return { ...cookies, [cookie[0]]: decodeURIComponent(cookie[1]) }; |
| 86 | + }, {})[key]; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private cookieWriter(name: string, value: string, options?: CookieOptionsArgs) { |
| 91 | + this.cookieString = this.buildCookieString(name, value, options); |
| 92 | + } |
| 93 | + |
| 94 | + private buildCookieString(name: string, value: string, options?: CookieOptionsArgs): string { |
| 95 | + const defaultOpts = this.defaultOptions || new CookieOptions(<CookieOptionsArgs>{ path: '/' }); |
| 96 | + const opts: CookieOptions = this.mergeOptions(defaultOpts, options); |
| 97 | + |
| 98 | + let expires = opts.expires; |
| 99 | + |
| 100 | + if (!value) { |
| 101 | + expires = 'Thu, 01 Jan 1970 00:00:00 GMT'; |
| 102 | + value = ''; |
| 103 | + } |
| 104 | + |
| 105 | + if (typeof expires === 'string') { |
| 106 | + expires = new Date(expires); |
| 107 | + } |
| 108 | + |
| 109 | + let str = encodeURIComponent(name) + '=' + encodeURIComponent(value); |
| 110 | + |
| 111 | + str += opts.path ? `;path=${opts.path}` : ''; |
| 112 | + str += opts.domain ? `;domain=${opts.domain}` : ''; |
| 113 | + str += expires ? `;expires=${expires.toUTCString()}` : ''; |
| 114 | + str += opts.secure ? ';secure' : ''; |
| 115 | + |
| 116 | + const cookieLength = str.length + 1; |
| 117 | + |
| 118 | + if (cookieLength > 4096) { |
| 119 | + console.log(`Cookie \'${name}\' possibly not set or overflowed because it was too large (${cookieLength} > 4096 bytes)!`); |
| 120 | + } |
| 121 | + |
| 122 | + return str; |
| 123 | + } |
| 124 | + |
| 125 | + private mergeOptions(defaultOpts: CookieOptions, providedOpts?: CookieOptionsArgs): CookieOptions { |
| 126 | + const newOpts = defaultOpts; |
| 127 | + |
| 128 | + if (providedOpts) { |
| 129 | + return newOpts.merge(new CookieOptions(providedOpts)); |
| 130 | + } |
| 131 | + |
| 132 | + return newOpts; |
| 133 | + } |
| 134 | + |
| 135 | + public get(key: string): string { |
| 136 | + return this.cookieReader(key); |
| 137 | + } |
| 138 | + |
| 139 | + public set(key: string, value: string, options?: CookieOptionsArgs): void { |
| 140 | + this.cookieWriter(key, value, options); |
| 141 | + } |
| 142 | + |
| 143 | + public remove(key: string, options?: CookieOptionsArgs): void { |
| 144 | + this.cookieWriter(key, undefined, options); |
| 145 | + } |
| 146 | +} |
0 commit comments