CF_8 [JS] Sensitive data

by mariomc

HTML

<pre id="json"></pre>

CSS

body {
  display: grid;
  place-items: center;
  height: 100vh;
}

TypeScript

let user = {
  name: "João",
  age: "32",
};

/////////////// 👇 ADD SOLUTION HERE   ///////////////////

// Creating a scope to save the original password outside the property. Other option would be to use another object property
{
	let hiddenPassword;
  Object.defineProperty(user, "password", {
    // Enumerable makes it not show in serialization
    enumerable: false,
    // Configurable makes it so that you can't change these properties
    configurable: false,
    
    // Define a getter function to always return *
    get: () {
			return Array(hiddenPassword.length).fill('*').join('');
    },
    
    // Set here the "hidden", scoped variable or property
    set: (value) {
			hiddenPassword = value;
    }
  });
}



///////////////  👆 ADD SOLUTION HERE    /////////////////

user.password = "qwerty";

document.querySelector("#json").innerHTML = `

user.name = ${user.name}
user.age = ${user.age}

user.password = ${user.password}

user = ${JSON.stringify(user, undefined, 1)}

`;