h-usersite/angular/src/app/services/cookies.service.ts
gofnnp 197c84f29c dev #12797
изменил расположение директорий и изменил файлы Jenkinsfile и gitignore
2022-10-11 10:40:59 +04:00

46 lines
1.2 KiB
TypeScript

import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class CookiesService {
constructor() {}
getItem(key: string): string|undefined {
const matches = document.cookie.match(new RegExp(
'(?:^|; )' + key.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + '=([^;]*)'
));
return matches ? decodeURIComponent(matches[1]) : undefined;
}
setCookie(key: string, value: string, options: any = {}): void {
const now = new Date();
const time = now.getTime();
const expireTime = time + 1000*36000*4*365*3;
now.setTime(expireTime);
options = { path: '/', ...options, expires: now.toUTCString() };
if (options.expires instanceof Date) {
options.expires = options.expires.toUTCString();
}
let updatedCookie = encodeURIComponent(key) + '=' + encodeURIComponent(value);
for (const optionKey of Object.keys(options)) {
updatedCookie += '; ' + optionKey;
const optionValue = options[optionKey];
if (optionValue !== true) {
updatedCookie += '=' + optionValue;
}
}
document.cookie = updatedCookie;
}
deleteCookie(key: string): void {
this.setCookie(key, '', { 'max-age': -1 });
}
}