My previous question was closed because it said this question would solve my problem. It didn't.
I am making a request to an API hosted locally to decrypt strings, then fill a table with other info.
For some reason, the request returns successfully but only the last row in the field gets filled in with the returned text.
There are no errors anywhere on the server or webpage.
server.js
const express = require('express')
const app = express()
const Datastore = require('nedb')
const { encrypt, decrypt } = require('./crypto')
app.set('view-engine', 'ejs')
app.use(express.urlencoded({ extended: false }))
app.get('/database', checkAuthenticated, async (req, res) => {
var userId = req._passport.session.user
var dbForUser = new Datastore('./passwords/' + userId + '.db')
dbForUser.loadDatabase()
dbForUser.find({}, (err, docs) => {
if (err) throw err;
return res.render('database.ejs', { db: docs })
})
})
app.get('/database/decrypt', checkAuthenticated, async (req, res) => {
res.send(await decrypt(JSON.parse(req.query.password)))
})
crypto.js
const crypto = require('crypto');
const algorithm = 'aes-256-ctr';
const secretKey = process.env.CRYPTO_KEY;
const iv = crypto.randomBytes(16);
const encrypt = (text) => {
const cipher = crypto.createCipheriv(algorithm, secretKey, iv);
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]);
return {
iv: iv.toString('hex'),
content: encrypted.toString('hex')
};
};
const decrypt = (hash) => {
const decipher = crypto.createDecipheriv(algorithm, secretKey, Buffer.from(hash.iv, 'hex'));
const decrpyted = Buffer.concat([decipher.update(Buffer.from(hash.content, 'hex')), decipher.final()]);
return decrpyted.toString();
};
module.exports = {
encrypt,
decrypt
};
database.ejs (most is omitted)
<body onload="display();">
<h1>My Passwords</h1>
<button onclick="newPassword();">Add Password <strong>+</strong></button><br><br>
<table style="width:100%" id="table" border="1" bgcolor="#FFFFFF">
<tr>
<th>Code</th>
<th>Link</th>
<th>Username</th>
<th>Password</th>
<th>Buttons</th>
</tr>
</table>
<script>
function display() {
// Displaying results timee
var results = <%-JSON.stringify(db)%>
if (results.length == 0) {
alert("No passwords in your database. Please create a new password in the following dalog.")
newPassword()
}
var table = document.getElementById('table')
function ajax(method, url, done) {
var xhr = new XMLHttpRequest();
xhr.open(method, url)
xhr.onreadystatechange = function() {
if (this.status == 200 && this.readyState == 4) {
done(null, xhr.responseText)
}
};
xhr.onerror = function() {
done(xhr.responseText)
};
xhr.send();
};
for (var i = 0; i < results.length; i++) {
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
ajax('GET', '/database/decrypt?password=' + JSON.stringify(results[i].password), function(err, data) {
if (err) throw err;
cell4.innerHTML = data + ` <button onclick="copy('`+ data + `')">Copy</button>`
})
cell1.innerHTML = results[i].code
cell2.innerHTML = results[i].link + " <a href=" + results[i].link + ">Open</a>"
cell3.innerHTML = results[i].name + ` <button onclick='copy("` + results[i].name + `")'>Copy</button>`
cell5.innerHTML = `<button onclick='deleteEntry("` + results[i].code + `")'>Delete</button> <button onclick='edit("` + results[i].code + `")'>Edit</button>`
}
document.getElementById('load-dots').style.display = "none";
}
</script>
</body>
Thank you!