I've been searching for this problem on Google for a while now and can't seem to understand the issue. I am using ejs. It does work when I manually type it on the address bar localhost:3000/home
this works, but using passport.js and setting home as my successRedirect, just gives me an error including the failureRedirect.
app.set("view engine", "ejs")
app.use(express.static("views"))
app.use(express.urlencoded({ extended: false }))
app.get("/home", (_, res) => res.render("home"))
app.get("/login", (_, res) => res.render("login"))
app.post("/login", passport.authenticate("local", {
successRedirect: "/home",
failureRedirect: "/login",
failureFlash: true,
}))
const response = await fetch("/login", {
method: "POST",
body: {
[username.id]: usernameValue,
[password.id]: passwordValue,
},
})
const result = await response.json()
if (result.id) localStorage.setItem("loginId", result.id)
if (result.href) window.location.href = result.href
I expected it to show the home page.
CodePudding user response:
Never mind, I'm so dumb. The problem was, I was parsing the result from fetch, which was trying to convert the html file to JSON.
fetch("/login", {
method: "POST",
body: {
[username.id]: usernameValue,
[password.id]: passwordValue,
},
}).then((response) => window.location.replace(response.url))
CodePudding user response:
try this
app.post('/login',
passport.authenticate('local', passport.authenticate("local", {
successRedirect: "/home",
failureRedirect: "/login",
failureFlash: true,
}),
function(req, res) {
res.redirect('/');
});