33 lines
909 B
TypeScript
33 lines
909 B
TypeScript
import { Injectable } from '@angular/core';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class CookieService {
|
|
public getCookie(name: string) {
|
|
const ca: string[] = document.cookie.split(';');
|
|
const caLen: number = ca.length;
|
|
const cookieName = `${name}=`;
|
|
let c: string;
|
|
|
|
for (let i = 0; i < caLen; i += 1) {
|
|
c = ca[i].replace(/^\s+/g, '');
|
|
if (c.indexOf(cookieName) === 0) {
|
|
return c.substring(cookieName.length, c.length);
|
|
}
|
|
}
|
|
return '';
|
|
}
|
|
|
|
public deleteCookie(name: string) {
|
|
this.setCookie(name, '', -1);
|
|
}
|
|
|
|
public setCookie(name: string, value: string, expireDays: number, path = '') {
|
|
const d: Date = new Date();
|
|
d.setTime(d.getTime() + expireDays * 24 * 60 * 60 * 1000);
|
|
const expires = `expires=${d.toUTCString()}`;
|
|
document.cookie = `${name}=${value}; ${expires}${path.length > 0 ? `; path=${path}` : ''}`;
|
|
}
|
|
}
|