base 64 encode and decode a string in angular (2+)

praveen kumar picture praveen kumar · Feb 1, 2017 · Viewed 163.2k times · Source

How to encode or decode a string in angular 2 with base64 ??? My front-end tool is Angular 2. I had a password string, before passing it to API I need to base64 encode. Since in service base64 encoded string will be decoded.

So I am looking for some base64 encode/decode library for Angular2/Typescript and some options.

Thanks!!!

Answer

Robby Cornelissen picture Robby Cornelissen · Feb 1, 2017

Use the btoa() function to encode:

console.log(btoa("password")); // cGFzc3dvcmQ=

To decode, you can use the atob() function:

console.log(atob("cGFzc3dvcmQ=")); // password