Search code examples
angularangular6

how to encrypt and decrypt URL in angular


In my project, two modules here(Staff, Agent ). I click Agent module list and URL shows Agent /list and update one Agent , URL passing id corresponding id . But i want encrypted data passing update and list in URL. So how to pass encrypted (user not understanding) values in Angular 6

return this.repository.postData('api/agent/list', requestObj);


Solution

  • Using atob() and btoa() functions can give you what you are looking for. For some of the btoa() functions you might need to do the toString() method if the value is non string ex.(NUMBER)

    Non Encrypted Routing

    //http://localhost:4200/api/id=1
    this.router.navigate(['/api',{ id: '1' },]);
    

    Encrypted Routing

    //http://localhost:4200/api/id=S2T2A3
    this.router.navigate(['/api',{ id: btoa('1') },]);
    

    How to get the id value on next page

    Non Decrypt ID

    this.id = this.route.snapshot.paramMap.get('id')!;
    

    Decrypt ID

    this.id = atob(this.route.snapshot.paramMap.get('id')!);