So I am trying to use implement argon2 in my login application which uses a MySQL database. I have managed to store the username and its hashed password in the database while receiving a promise handling error. But I am unable to login with the same username and password by passing parameters via Postman since I get the same error.
Here is my code so far:
server.js
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
app.use(bodyParser.json({ type: "application/json" }));
app.use(bodyParser.urlencoded({ extended: true }));
app.set("port", 8080);
const argon2 = require("argon2");
const Pool = require("mysql2").createPool;
const config = {
host: "localhost",
user: "root",
password: "XXXXX",
database: "XXXXX"
};
const pool = new Pool(config);
app.post("/login", async (req, res) => {
console.log(req.body);
const username = req.body.username;
const password = req.body.password;
try {
const query = "SELECT password from adminlogin where username = ?";
const result = await pool.query(query, [username]);
if (result.rowCount == 1) {
console.log(result.rows[0].password);
if (await argon2.verify(result.rows[0].password, password)) {
res.json("Log In successful");
} else {
res.json("Password incorrect");
}
} else {
res.json("username not found");
}
} catch (err) {
console.log("ERROR " + err);
}
});
app.post("/create", async (req, res) => {
let hash;
const username = req.body.username;
const password = req.body.password;
try {
hash = await argon2.hash(password, "abcdefghijklmnop");
console.log("HASH " + hash);
const query = "INSERT INTO adminlogin (username, password) VALUES (?, ?)";
const result = await pool.query(query, [username, hash]);
//console.log(result);
if (result.rowCount == 1) {
res.json("User created");
} else {
res.json("User not created");
}
} catch (err) {
console.log("ERROR " + err);
if (err.message.search("duplicate") != -1) {
res.json("Username taken");
}
}
});
Error Messages:
You have tried to call .then(), .catch(), or invoked await on the result of query that is not a promise, which is a programming error. Try calling con.promise().query(), or require('mysql2/promise') instead of 'mysql2' for a promise-compatible version of the query interface. To learn how to use async/await or Promises check out documentation at https://www.npmjs.com/package/mysql2#using-promise-wrapper, or the mysql2 documentation at https://github.com/sidorares/node-mysql2/tree/master/documentation/Promise-Wrapper.md ERROR Error: You have tried to call .then(), .catch(), or invoked await on the result of query that is not a promise, which is a programming error. Try calling con.promise().query(), or require('mysql2/promise') instead of 'mysql2' for a promise-compatible version of the query interface. To learn how to use async/await or Promises check out documentation at https://www.npmjs.com/package/mysql2#using-promise-wrapper, or the mysql2 documentation at https://github.com/sidorares/node-mysql2/tree/master/documentation/Promise-Wrapper.md