Quantcast
Channel: Active questions tagged config - Stack Overflow
Viewing all articles
Browse latest Browse all 5049

Node.js - Storing password in env variable

$
0
0

I am creating admin credentials for my app, the model is quite simple, it has username and password properties and some validation as well. I have a function where I populate some information to the db.

async function init() {  const admin = new Admin({    username: "admin",    password: "12345"  });  const salt = await bcrypt.genSalt(10);  admin.password = await bcrypt.hash(admin.password, salt);  try {    await admin.save({ username: admin.username });  } catch (ex) {    console.log(ex.message);  }}

However, with this approach I am saving the password in my source code and I don't want that. I was thinking that maybe I could store my admin password in an environment variable using the config package.

I tried the following:

  • Created a default.json file:

    { "adminPassword": "" }

  • Created a custom-environment-variables.json file:

    { "adminPassword": "fifteen_adminPassword" }

  • Then modified my code to this:

const admin = new Admin({ username: "admin", password: config.get("adminPassword") });

Finally I set the env variable export fifteen_adminPassword=12345 but this fails as invalid password when I'm authenticating.


Viewing all articles
Browse latest Browse all 5049

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>