npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-encrypt-cookie

v1.1.7

Published

encrypting and decrypting cookies in angular

Downloads

101

Readme

NgxEncryptCookie

This library was generated with Angular CLI version 8.2.0.

Click here to go to blog - coderAdda

Installation

npm i ngx-encrypt-cookie --save

Then...

Inject either in module or component

  • you can import in app.module.ts or in your custom module shared service
  • you can import in component level to use as singleton service.

Usage

....
import {NgxEncryptCookieService} from 'ngx-secure-cookie'

@Component({
  ......
  providers: [NgxEncryptCookieService]
})
export class AppComponent {

  constructor(
    private cookie:NgxEncryptCookieService,
  ){

    var key = this.cookie.generateKey();

    this.cookie.set("test","test value",true,key);


    var otherKey = this.cookie.generateKey("256/32","hello all");

    this.cookie.set("customKey","test value without encryption",true,otherKey);

    var val = this.cookie.get("test",true,key);   // decrypted value
    var val_1 = this.cookie.get("customKey",false)  //encrypted value

    var val_2 = this.cookie.getAll();
    //returns all cookies - here it will return encrypted values also if cookie is having

    var val_3 = this.cookie.getAll(true,key);
     // returns all values - here if a cookie is having encrypted value it will decrypt.
    this.cookie.delete("test");

    this.cookie.deleteAll("/");
    this.cookie.deleteAll();

  }
}

Methods :

generateKey(keySize?: string, passPhrase?: string)


var key = this.cookie.generateKey("128/32","hello");
  • @params keySize - to generate key of given size. Available keySize - 128/32, 256/32, 512/32 - Default(128 / 32)
  • @params passPhrase - using passPhrase key will be generated of given keySize.

check( name: string ): boolean;


const cookieExists: boolean = this.cookie.check('test');
  • @params name - cookie name
  • @params encryption - if cookie value encrypted pass true othwerwise false
  • @params key string - pass key value if encrypted=true
  • Checks if a cookie with the give name can be accessed or found.

get( name: string,encryption?:boolean ): string;


const value: string = cookieService.get('test',false);
  • @params name - Gets the value of the - with the specified name.

getAll(encryption?:boolean,key?:string): { [key: string]: string }


const cookies = this.cookie.getAll(true,key);  // use if you have one encrypted value

const normalCookies = this.cookie.getAll()    // use this if you have zero encrypted value
  • @param encryption boolean - if cookies having encrypted values. if you need encrypted value pass false otherwise pass the true to get decrypted value and if you set encryption=true , then dont pass key value it is unneccessary.
  • @param key string - pass generated key or user defined key onlyif encrypted=true.
  • Returns a map of key-value pairs for cookies that can be accessed.

set( name: string, value: string, encrypt:boolean, key?:string, expires?: number | Date, path?: string, domain?: string, secure?: boolean, sameSite?: 'Lax' | 'Strict' | 'None' ): void;


this.cookie.set("test","test value",false);
this.cookie.set("test1","test value",true,key);
  • Sets a cookie with the specified name and value.pass true or false to either encrypt values or not. It is good practice to specify a path. If you are unsure about the path value, use '/'. If no path or domain is explicitly defined, the current location is assumed. sameSite defaults to Lax.

#f03c15 NOTE:

  • For security reasons, it is not possible to define cookies for other domains. Browsers do not allow this. Read this and this StackOverflow answer for a more in-depth explanation.

  • Browsers do not accept cookies flagged sameSite = 'None' if secure flag isn't set as well. NgxSecureCookies will override the secure flag to true if sameSite='None'.

delete( name: string, path?: string, domain?: string, secure?: boolean, sameSite: 'Lax' | 'None' | 'Strict' = 'Lax' ): void;


this.cookie.delete('test');
  • Deletes a cookie with the specified name. It is best practice to always define a path. If you are unsure about the path value, use '/'.

#f03c15 Important:

  • For security reasons, it is not possible to delete cookies for other domains. Browsers do not allow this. Read this and this StackOverflow answer for a more in-depth explanation.

deleteAll( path?: string, domain?: string, secure?: boolean, sameSite: 'Lax' | 'None' | 'Strict' = 'Lax' ): void;


this.cookie.deleteAll("/");
  • Deletes all cookies that can access. It is always a best practice to define a path to delete. If you are not sure about the path where the cookie store pass '/'.

Licence:

  • MIT

About

  • Name : Chaitanya
  • Git : https://github.com/chaitanyasairam-d
  • Blog : https://coder-adda.blogspot.com/2022/02/ngx-encrypt-cookie.html