Skip to content

Instantly share code, notes, and snippets.

@nhumphrey2
Created December 10, 2018 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhumphrey2/1be50f01328c88883bafe9c5bbc86f83 to your computer and use it in GitHub Desktop.
Save nhumphrey2/1be50f01328c88883bafe9c5bbc86f83 to your computer and use it in GitHub Desktop.
Validating User Authentication Requests Node.js
app.get('/auth', (req, res) => {
let username = req.query.username || '';
const password = req.query.password || '';
username = username.replace(/[!@#$%^&*]/g, '');
if (!username || !password || !users[username]) {
return res.sendStatus(400);
}
const { salt, hash } = users[username];
const encryptHash = crypto.pbkdf2Sync(password, salt, 10000, 512, 'sha512');
if (crypto.timingSafeEqual(hash, encryptHash)) {
res.sendStatus(200);
} else {
res.sendStatus(401);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment