import { Inject , Injectable , PLATFORM_ID } from '@angular/core' ; import { DOCUMENT , isPlatformBrowser } from '@angular/common' ; @ Injectable ( { providedIn : 'root' , } ) export class CookieService { private readonly documentIsAccessible : boolean ; constructor ( @ Inject ( DOCUMENT ) private document , @ Inject ( PLATFORM_ID ) private platformId ) { this . documentIsAccessible = isPlatformBrowser ( this . platformId ) ; } param name returns author : Stepan Suvorov
     *  since private static getCookieRegExp ( name ) { const escapedName = name . replace ( /([\[\]\{\}\(\)\|\=\;\+\?\,\.\*\^\$])/gi , '\\$1' ) ; return new RegExp ( '(?:^' + escapedName + '|;\\s*' + escapedName + ')=(.*?)(?:;|$)' , 'g' ) ; } param encodedURIComponent returns author : Stepan Suvorov
     *  since private static safeDecodeURIComponent ( encodedURIComponent ) { try { return decodeURIComponent ( encodedURIComponent ) ; } catch { return encodedURIComponent ; } } /**
     * Return `true` if  Document is accessible, otherwise return `false`
     *
     *  param name returns author : Stepan Suvorov
     *  since check ( name ) { if ( ! this . documentIsAccessible ) { return false ; } name = encodeURIComponent ( name ) ; const regExp = CookieService . getCookieRegExp ( name ) ; return regExp . test ( this . document . cookie ) ; } param name returns author : Stepan Suvorov
     *  since get ( name ) { if ( this . documentIsAccessible && this . check ( name ) ) { name = encodeURIComponent ( name ) ; const regExp = CookieService . getCookieRegExp ( name ) ; const result = regExp . exec ( this . document . cookie ) ; return result [ 1 ] ? CookieService . safeDecodeURIComponent ( result [ 1 ] ) : '' ; } else { return '' ; } } returns author : Stepan Suvorov
     *  since getAll ( ) { if ( ! this . documentIsAccessible ) { return { } ; } const cookies = { } ; const document = this . document ; if ( document . cookie && document . cookie !== '' ) { document . cookie . split ( ';' ) . forEach ( ( currentCookie ) => { const [ cookieName , cookieValue ] = currentCookie . split ( '=' ) ; cookies [ CookieService . safeDecodeURIComponent ( cookieName . replace ( /^ / , '' ) ) ] = CookieService . safeDecodeURIComponent ( cookieValue ) ; } ) ; } return cookies ; } param name param value param expires param path param domain param secure param sameSite author : Stepan Suvorov
     *  since set ( name , value , expires ? , path ? , domain ? , secure ? , sameSite ? ) ; param name param value param options author : Stepan Suvorov
     *  since set ( name , value , options ? ) ; set ( name , value , expiresOrOptions ? , path ? , domain ? , secure ? , sameSite ? ) { if ( ! this . documentIsAccessible ) { return ; } if ( typeof expiresOrOptions === 'number' || expiresOrOptions instanceof Date || path || domain || secure || sameSite ) { const optionsBody = { expires : expiresOrOptions , path , domain , secure , sameSite : sameSite ? sameSite : 'Lax' , } ; this . set ( name , value , optionsBody ) ; return ; } let cookieString = encodeURIComponent ( name ) + '=' + encodeURIComponent ( value ) + ';' ; const options = expiresOrOptions ? expiresOrOptions : { } ; if ( options . expires ) { if ( typeof options . expires === 'number' ) { const dateExpires = new Date ( new Date ( ) . getTime ( ) + options . expires * 1000 * 60 * 60 * 24 ) ; cookieString += 'expires=' + dateExpires . toUTCString ( ) + ';' ; } else { cookieString += 'expires=' + options . expires . toUTCString ( ) + ';' ; } } if ( options . path ) { cookieString += 'path=' + options . path + ';' ; } if ( options . domain ) { cookieString += 'domain=' + options . domain + ';' ; } if ( options . secure === false && options . sameSite === 'None' ) { options . secure = true ; console . warn ( `[ngx-cookie-service] Cookie ${ name } was forced with secure flag because sameSite=None.` + `More details : https://github.com/stevermeister/ngx-cookie-service/issues/86#issuecomment-597720130` ) ; } if ( options . secure ) { cookieString += 'secure;' ; } if ( ! options . sameSite ) { options . sameSite = 'Lax' ; } cookieString += 'sameSite=' + options . sameSite + ';' ; this . document . cookie = cookieString ; } param name param path param domain param secure param sameSite author : Stepan Suvorov
     *  since delete ( name , path ? , domain ? , secure ? , sameSite = 'Lax' ) { if ( ! this . documentIsAccessible ) { return ; } const expiresDate = new Date ( 'Thu, 01 Jan 1970 00:00:01 GMT' ) ; this . set ( name , '' , { expires : expiresDate , path , domain , secure , sameSite } ) ; } param path param domain param secure param sameSite author : Stepan Suvorov
     *  since deleteAll ( path ? , domain ? , secure ? , sameSite = 'Lax' ) { if ( ! this . documentIsAccessible ) { return ; } const cookies = this . getAll ( ) ; for ( const cookieName in cookies ) { if ( cookies . hasOwnProperty ( cookieName ) ) { this . delete ( cookieName , path , domain , secure , sameSite ) ; } } } }