I am new to ExpressJS. I have made an API which inserts user's email and encrypted password into MongoDB. Now I want to decrypt the password which is stored on MongoDB and compare it with the password user has entered. I have no idea how to do that, so any help would be appreciated. Here is my code :
const express = require("express");
const router = express.Router();
var CryptoJS = require("crypto-js");
// Importing Model
const User = require('../models/model.js');
// Find User
router.post('/find' , (req , res) => { // This is the method which checks the DB for user's email and password
User.find({email : req.body.email , password : req.body.password})
.then((users) => {
if (users.length != 0) {
res.json({result : true});
}
else {
res.json({result : false});
}
})
});
// Add User
router.post('/' , (req , res) => {
var ciphertext = CryptoJS.AES.encrypt(req.body.password , 'secret key 123').toString(); // Encryption
const newUser = new User({
email : req.body.email,
password : ciphertext,
})
newUser.save().then((user) => res.json(user))
});
// Exporting Router
module.exports = router;
This is the method provided by crypto-js for decryption :
var bytes = CryptoJS.AES.decrypt(ciphertext, 'secret key 123');
var originalText = bytes.toString(CryptoJS.enc.Utf8);
I need to decrypt using this code in my POST (Find User) Route.