Skip to content

Instantly share code, notes, and snippets.

@ackuser
Forked from bvandenbon/cookieservice.service.ts
Created January 31, 2019 11:01
Show Gist options
  • Select an option

  • Save ackuser/7ee0e8edb7686ece6ebf6847de70c940 to your computer and use it in GitHub Desktop.

Select an option

Save ackuser/7ee0e8edb7686ece6ebf6847de70c940 to your computer and use it in GitHub Desktop.
CookieService
import { Injectable } from '@angular/core';
@Injectable()
export class CookieService {
constructor() {
}
set(key: string, value: string): void;
set(key: string, value: string, expires: Date): void;
set(key: string, value: string, expires?: Date): void {
let cookieValue = `${key}=${value}`;
if (expires) cookieValue += `;expires='${expires.toUTCString()}'`
document.cookie = cookieValue;
}
setWithExpiryInYears(key: string, value: string, expires: number) {
this.setWithExpiryInDays(key, value, expires * 365);
}
setWithExpiryInDays(key: string, value: string, expires: number) {
this.setWithExpiryInHours(key, value, expires * 24);
}
setWithExpiryInHours(key: string, value: string, expires: number) {
this.setWithExpiryInMinutes(key, value, expires * 60);
}
setWithExpiryInMinutes(key: string, value: string, expires: number) {
this.setWithExpiryInSeconds(key, value, expires * 60);
}
setWithExpiryInSeconds(key: string, value: string, expires: number) {
this.setWithExpiryInMiliseconds(key, value, expires * 1000);
}
setWithExpiryInMiliseconds(key: string, value: string, expires: number) {
var expireDate = new Date();
var time = expireDate.getTime() + expires;
expireDate.setTime(time);
this.set(key, value, expireDate);
}
get(key: string): string {
const decodedCookie: string = decodeURIComponent(document.cookie);
const pairs: string[] = decodedCookie.split(/;\s*/);
const prefix = `${key}=`;
for (const pair of pairs) {
if (pair.indexOf(prefix) == 0) {
return pair.substring(prefix.length);
}
}
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment