Skip to main content

--description--

Going back to the information security section, you may remember that storing plaintext passwords is never okay. Now it is time to implement BCrypt to solve this issue.

bcrypt@~5.0.0 has already been added as a dependency, so require it in your server. You will need to handle hashing in 2 key areas: where you handle registering/saving a new account, and when you check to see that a password is correct on login.

Currently on your registration route, you insert a user's plaintext password into the database like so: password: req.body.password. Hash the passwords instead by adding the following before your database logic: const hash = bcrypt.hashSync(req.body.password, 12);, and replacing the req.body.password in the database saving with just password: hash.

On your authentication strategy, you check for the following in your code before completing the process: if (password !== user.password) return done(null, false);. After making the previous changes, now user.password is a hash. Before making a change to the existing code, notice how the statement is checking if the password is not equal then return non-authenticated. With this in mind, change that code to look as follows to properly check the password entered against the hash:

if (!bcrypt.compareSync(password, user.password)) { 
return done(null, false);
}

That is all it takes to implement one of the most important security features when you have to store passwords.

Submit your page when you think you've got it right. If you're running into errors, you can check out the project completed up to this point.

--hints--

BCrypt should be a dependency.

async (getUserInput) => {
const url = new URL("/_api/package.json", getUserInput("url"));
const res = await fetch(url);
const packJson = await res.json()
assert.property(
packJson.dependencies,
'bcrypt',
'Your project should list "bcrypt" as a dependency'
);
}

BCrypt should be correctly required and implemented.

async (getUserInput) => {
const url = new URL("/_api/server.js", getUserInput("url"));
const res = await fetch(url);
const data = await res.text();
assert.match(
data,
/require.*("|')bcrypt\1/gi,
'You should have required bcrypt'
);
assert.match(
data,
/bcrypt.hashSync/gi,
'You should use hash the password in the registration'
);
assert.match(
data,
/bcrypt.compareSync/gi,
'You should compare the password to the hash in your strategy'
);
}