How to check if repeat password equal to password in Node.js and Express

1 Answer

0 votes
const express = require('express');
const { check, validationResult } = require('express-validator');

const router = express.Router();

router.post(
  '/',
  [
      check('repeat_password')
        .custom((value, { req }) => value === req.body.password)
        .withMessage('Passwords do not match'),
  ],
  (req, res) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }
    res.send('User details okay');
  }
);


module.exports = router;

// Postman 

/*
{
	"password": "aaaaaa1!",
	"repeat_password": "aaaaaa1!"
}
*/

/*

run:

User details okay

*/

 



answered May 11, 2020 by avibootz
...