Created
February 8, 2014 07:44
-
-
Save joeylin/8878137 to your computer and use it in GitHub Desktop.
nodejs input password in Terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function askPassword(callback) { | |
var stdin = process.openStdin(), | |
stdio = process.binding("stdio"); | |
stdio.setRawMode(); | |
console.log('Enter your password:'); | |
var password = ""; | |
stdin.on("data", function(c) { | |
c = c + ""; | |
switch (c) { | |
case "\n": case "\r": case "\u0004": | |
stdio.setRawMode(false); | |
stdin.pause(); | |
callback(password); | |
break; | |
case "\u0003": | |
process.exit(); | |
break; | |
default: | |
password += c; | |
break; | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! This was really helpful!