I'm working with the passport-local authentication and I have it mostly working in a very simple React app running off of a server.js file. I have my simple LocalStrategy function setup to return success if username === password
, otherwise it's a failure.
I get that after passport.authenticate fails the login test, that that the next function in line does not execute. What I don't know is how to get the error message I passed into the done
method in the LocalStrategy function.
I've got the app running without errors at this URL in codesandbox.
https://codesandbox.io/s/github/pkellner/next-custom-server-pkellner
The server.js is as follows:
passport.use(
new LocalStrategy(function(username, password, done) {
console.log(
`LocalStrategy Function: username:${username} password:${password}`
);
if (username === password) {
return done(
null,
{ username: username },
{
message: "where does this message come out?"
}
);
}
return done(null, false, {
message: "failure, nuclear meltdown"
});
})
);
app
.prepare()
.then(() => {
const server = express();
server.use(passport.initialize());
server.use(
bodyParser.urlencoded({
extended: true
})
);
server.get("/login", (req, res) => {
console.log("in server.js login called...");
return app.render(req, res, "/login");
});
server.post(
"/login",
passport.authenticate("local", {
failureRedirect: "/error",
successRedirect: "/success"
}),
(req, res) => {
console.log(
`success: server.post login req.user.username:${
req.user.username
} req.authInfo.message:${req.authInfo.message}`
);
}
);
server.get("*", (req, res) => {
return handle(req, res);
});
server.listen(3000, err => {
if (err) throw err;
console.log("> Ready on http://localhost:3000...");
});
})
.catch(ex => {
console.error(ex.stack);
process.exit(1);
});
You can pass a callback to passport.authenticate()
like so:
passport.authenticate("local", {
failureRedirect: "/error",
successRedirect: "/success"
}, (err, user, options) => {
console.log(options) // options will be the complete object you pass in done()
});
You can find the documentation in Custom Callback
section from here.