Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs')
- const SecurePassword = require('secure-password')
- const pwd = SecurePassword()
- const command = process.argv[2]
- const inputUsername = process.argv[3]
- const inputPassword = Buffer.from(process.argv[4])
- if (command === 'createUser') return createUser(inputUsername, inputPassword)
- if (command === 'verify') return verify(inputUsername, inputPassword)
- function createUser (username, password) {
- pwd.hash(password, (err, hash) => {
- if (err) throw err
- fs.writeFileSync('./users.csv', `${username},${password}`)
- console.log('user created')
- })
- }
- function verify (username, password) {
- pwd.hash(password, (err, hash) => {
- if (err) throw err
- const userData = fs.readFileSync('./users.csv').toString().split(',')
- const foundUsername = userData[0]
- const foundPassword = Buffer.from(userData[1])
- if (username === foundUsername) {
- return pwd.verify(foundPassword, hash, (err, result) => {
- if (err) throw err
- if (result === SecurePassword.INVALID) return console.log('wrong password')
- if (result === SecurePassword.VALID) return console.log('ACCESS GRANTED')
- if (result === SecurePassword.VALID_NEEDS_REHASH) {
- console.log('making yer pw safer')
- pwd.hash(userPassword, (err, improvedHash) => {
- if (err) console.error('authd but yer pw aint safer')
- })
- }
- })
- }
- return console.log('no user found')
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement