diff --git a/README.md b/README.md index 1910a66..5dbbcd6 100644 --- a/README.md +++ b/README.md @@ -216,6 +216,18 @@ Allows you to decide which name will be used for storing the cookie in the clien The previous example will generate a `myShinyCookieLaw=true` as soon as the user dismiss the banner. +### expire + +| Type | Default value | Description | +| --- | --- | --- | +| number | - | Set a the cookie expiration time (in days) | + +###### Example + +```html +I'm gonna expire in 1 week! +``` + ## Properties | Name | Type | Description | diff --git a/demo/app.component.ts b/demo/app.component.ts index 346bd9d..c7ea46c 100644 --- a/demo/app.component.ts +++ b/demo/app.component.ts @@ -5,7 +5,7 @@ import { } from '@angular/core'; @Component({ - selector: 'demo-app', + selector: 'cookie-demo-app', template: `

Angular2-Cookie-Law

Demo page @@ -25,8 +25,8 @@ import { - - Allo! This is my awesome cookie-law message. + + Allo! I'm expiring in one week!. Click here for more info diff --git a/demo/index.html b/demo/index.html index 3957bd6..0f31afb 100644 --- a/demo/index.html +++ b/demo/index.html @@ -10,9 +10,9 @@ angular2-cookie-law demo app - + Loading... - + diff --git a/src/cookie-law.component.ts b/src/cookie-law.component.ts index 08bd326..b9d2511 100644 --- a/src/cookie-law.component.ts +++ b/src/cookie-law.component.ts @@ -53,10 +53,15 @@ export class CookieLawComponent implements OnInit { @Input() public position: CookieLawPosition; + @Input() + public expiration: number; + @Output() public isSeen = new EventEmitter(); - constructor (private _service: CookieLawService) { } + constructor (private _service: CookieLawService) { + this.name = 'cookieLawSeen'; // set a default cookie name if not provided + } public get cookieLawSeen(): boolean { return this._service.seen(this.name); @@ -67,7 +72,7 @@ export class CookieLawComponent implements OnInit { } public hasBeenDismissed(): void { - this._service.storeCookie(this.name); + this._service.storeCookie(this.name, this.expiration); this.seen = true; this.isSeen.emit(true); } diff --git a/src/cookie-law.service.spec.ts b/src/cookie-law.service.spec.ts index 815dfef..20eb545 100644 --- a/src/cookie-law.service.spec.ts +++ b/src/cookie-law.service.spec.ts @@ -13,12 +13,20 @@ describe('CookieLawService', () => { }); it('#seen should now have a cookie stored', () => { - service.storeCookie(); + service.storeCookie('cookieLawSeen'); expect(service.seen()).toBe(true); expect(service.seen('cookieLawSeen')).toBe(true); }); + it('set an expiration time', () => { + service.storeCookie('cookieLawSeen', 1); + + expect(service.seen()).toBe(true); + expect(service.seen('cookieLawSeen')).toBe(true); + expect(document.cookie.match('cookieLawSeen').indexOf('cookieLawSeen')).not.toBe(-1); + }); + it('should stored different cookie names', () => { service.storeCookie('testCookie'); diff --git a/src/cookie-law.service.ts b/src/cookie-law.service.ts index 583ddbb..9a43f5c 100644 --- a/src/cookie-law.service.ts +++ b/src/cookie-law.service.ts @@ -10,12 +10,12 @@ import { Injectable } from '@angular/core'; @Injectable() export class CookieLawService { - public seen(cookieName?: string): boolean { - return this.cookieExisits(cookieName || 'cookieLawSeen'); + public seen(cookieName: string = 'cookieLawSeen'): boolean { + return this.cookieExisits(cookieName); } - public storeCookie(cookieName?: string): void { - return this.setCookie(cookieName || 'cookieLawSeen'); + public storeCookie(cookieName: string, expiration?: number): void { + return this.setCookie(cookieName, expiration); } /** @@ -46,7 +46,13 @@ export class CookieLawService { * * @param {string} name [the name for the cookie] */ - private setCookie(name: string): void { - document.cookie = encodeURIComponent(name) + '=true; path=/'; + private setCookie(name: string, expiration?: number): void { + const date = new Date(); + let expires; + + date.setTime(date.getTime() + expiration * 86400000); + expires = '; expires=' + date.toUTCString(); + + document.cookie = encodeURIComponent(name) + '=true; path=/' + expires; } } diff --git a/yarn.lock b/yarn.lock index e06e88d..c3bbf4d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1112,13 +1112,13 @@ debug@2.3.3: dependencies: ms "0.7.2" -debug@2.6.7: +debug@2.6.7, debug@^2.2.0: version "2.6.7" resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.7.tgz#92bad1f6d05bbb6bba22cca88bcd0ec894c2861e" dependencies: ms "2.0.0" -debug@2.6.8, debug@^2.2.0, debug@^2.6.8: +debug@2.6.8, debug@^2.6.8: version "2.6.8" resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.8.tgz#e731531ca2ede27d188222427da17821d68ff4fc" dependencies: @@ -3533,27 +3533,27 @@ read-pkg@^1.0.0: normalize-package-data "^2.3.2" path-type "^1.0.0" -"readable-stream@^2.0.0 || ^1.1.13", readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.5, readable-stream@^2.0.6, readable-stream@~2.0.0, readable-stream@~2.0.5: - version "2.0.6" - resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.0.6.tgz#8f90341e68a53ccc928788dacfcd11b36eb9b78e" +"readable-stream@^2.0.0 || ^1.1.13", readable-stream@^2.1.4, readable-stream@^2.2.6, readable-stream@^2.2.9: + version "2.2.10" + resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.2.10.tgz#effe72bb7c884c0dd335e2379d526196d9d011ee" dependencies: core-util-is "~1.0.0" inherits "~2.0.1" isarray "~1.0.0" process-nextick-args "~1.0.6" - string_decoder "~0.10.x" + safe-buffer "^5.0.1" + string_decoder "~1.0.0" util-deprecate "~1.0.1" -readable-stream@^2.1.4, readable-stream@^2.2.6, readable-stream@^2.2.9: - version "2.2.10" - resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.2.10.tgz#effe72bb7c884c0dd335e2379d526196d9d011ee" +readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.5, readable-stream@^2.0.6, readable-stream@~2.0.0, readable-stream@~2.0.5: + version "2.0.6" + resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.0.6.tgz#8f90341e68a53ccc928788dacfcd11b36eb9b78e" dependencies: core-util-is "~1.0.0" inherits "~2.0.1" isarray "~1.0.0" process-nextick-args "~1.0.6" - safe-buffer "^5.0.1" - string_decoder "~1.0.0" + string_decoder "~0.10.x" util-deprecate "~1.0.1" readable-stream@~1.0.2: