You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"INSERT INTO users (username, password) VALUES ($1, $2)",
364
+
[req.body.username, hashedPassword],
365
+
);
366
+
res.redirect("/");
384
367
});
385
368
```
386
369
387
370
We don't need to modify any of its options, as the defaults all meet the [password storage recommendations set by OWASP](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#introduction) (Open Worldwide Application Security Project). Now in our `POST/login` middleware, we can also use argon2 to verify the submitted password against the stored salted hash:
388
371
389
372
```javascript
390
373
app.post("/login", async (req, res, next) => {
391
-
try {
392
-
const { rows } =awaitpool.query(
393
-
"SELECT * FROM users WHERE username = $1",
394
-
[req.body.username],
395
-
);
396
-
constuser= rows[0];
397
-
398
-
// argon2.verify requires an argon2 hash as its first arg
399
-
// so we can't just pass in `undefined` if no user exists.
400
-
// The hash itself doesn't matter as long as it's a valid argon2 hash
401
-
// since this is to prevent timing attacks if no user is found
402
-
constisMatchingPassword=awaitargon2.verify(
403
-
user?.password??process.env.FALLBACK_HASH,
404
-
req.body.password,
405
-
);
406
-
if (user && isMatchingPassword) {
407
-
req.session.userId=user.id;
408
-
req.session.save((err) => {
409
-
if (err) {
410
-
next(err);
411
-
} else {
412
-
res.redirect("/");
413
-
}
414
-
});
415
-
} else {
416
-
res.render("login", {
417
-
error:"Incorrect username or password",
418
-
});
419
-
}
420
-
} catch(err) {
421
-
next(err);
374
+
const { rows } =awaitpool.query(
375
+
"SELECT * FROM users WHERE username = $1",
376
+
[req.body.username],
377
+
);
378
+
constuser= rows[0];
379
+
380
+
// argon2.verify requires an argon2 hash as its first arg
381
+
// so we can't just pass in `undefined` if no user exists.
382
+
// The hash itself doesn't matter as long as it's a valid argon2 hash
383
+
// since this is to prevent timing attacks if no user is found
0 commit comments