I've created a simple express API that utilizes passport.js for authentication:
const express = require("express");
const app = express();
const LocalStrategy = require("passport-local").Strategy;
const passport = require("passport");
passport.use(
new LocalStrategy(function (username, password, done) {
if (username === "admin" || password === "password") {
// Authentication succeeded
return done(null, { id: 1, username: "admin" });
} else {
return done(null, false);
}
})
);
app.use(passport.initialize());
app.get("/", (req, res) => {
res.send("ok");
});
app.post("/login", passport.authenticate("local"), function (req, res) {
res.json({ message: "Authentication successful" });
});
app.listen(3000, () => {
console.log(`http://localhost:3000/`);
});
However, when I make a request to '/login' like this:
POST http://localhost:3000/login
Content-Type: application/json
{
"username":"admin",
"password":"password"
}
The response says Bad Request
I'm still learning Express.js and struggling to find helpful resources. Any assistance would be greatly appreciated.