0

I want to know how can I compare values received from user input with values stored into file.txt in Node.js.

update : I do not need to store passwords in plain text. The text file is used only for validation of the admin credentials. This meaning that The credentials are changeable through the text file and the admin has to know the credentials in order to log in. It may not make sense but it is for learning purposes.

3
  • It sounds like you're planning on storing passwords in plain text. You should be storing salted hashes of passwords, not plain text. Commented Feb 27, 2019 at 0:31
  • No I do not need to store passwords in plain text. The text file is used only for validation of the admin credentials. This meaning that The credentials are changeable through the text file and the admin has to know the credentials in order to log in. It may not make sense but it is for learning purposes. Commented Feb 27, 2019 at 0:36
  • It sure looks like your example text file contains the email [email protected] and the password admin. And the answer from Nguyễn Việt Đức that you said was "Awesome" reads the password out of the file in plain text. You're right that so far it does not make sense to me. Commented Feb 27, 2019 at 4:02

1 Answer 1

1

For your learning purpose, simply compare strings. If your userInputPassword is in plain text and password stored in admin.txt is hashed, then you have to decrypt your password in admin.txt before comparing.

const fs = require('fs');
const readline = require('readline').createInterface({
 input: process.stdin,
 output: process.stdout
});
const read = fs.readFileSync('admin.txt', 'utf8');
console.log(read);
readline.question(`What's your email? `, (email) => {
 const userInputEmail = email.trim();
 console.log(`Email: ${email}`)
   readline.question(`What's your password? `, (password) => {
     const userInputPassword = password.trim();
     console.log(`Password: ${password}`);
     const [correctEmail, correctPassword] = read.split('\n').map(s => s.trim());
     if (userInputEmail === correctEmail && userInputPassword === correctPassword) console.log('Logged in');
     else console.log('Email or password is incorrect');
     readline.close();
   });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome. It was a little confusing for me but it looks great now. So how can I do that if the credentials are correct my cli app continues with some other process like showing a menu options for the admin ? For example let it be 4 options to choose from. Shall I do it with inquirer or anything else. Your help is much appreciated. Thanks
For command line applications, I recommend using a library like commander rather than trying to create it yourself because you will have to nest many callbacks together.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.