I've looked at several similar questions on here and none seem answer my specific question. But I am just trying to pass back a custom message on login error and can't figure out how to get it.
So here is my local strategy code in my passport.js file:
passport.use(
new LocalStrategy((username, password, done) => {
User.userSearch(username)
.then((user) => {
console.log({ user });
if (!user.allow_login) {
// don't allow login, send back message
return done(null, false, {
message: 'Login disabled right now.',
});
} else if (user?.password) {
...
And then the login route in my user gateway:
router.post(
'/login',
(req, res, next) => {
if (req.isAuthenticated()) {
req.session.touch();
}
next();
},
passport.authenticate('local', {
failureRedirect: '/login',
failureFlash: true,
}),
(req, res) => {
console.log({ res });
console.log({ req });
// if you made it here, the user is real and authentic
// check rememberMe and set cookie age accordingly
if (req.body.rememberMe) {
req.session.cookie.maxAge = 30 * 24 * 60 * 60 * 1000; // Cookie expires after 30 days
} else {
req.session.cookie.expires = false; // Cookie expires at end of session
}
// create new user object with email, customer name, and type
const user = {
email: req.user.email,
type: req.user.type,
first: req.user.first_name,
last: req.user.last_name,
filled_in: req.user.filled_in_basic_profile,
};
res.status(200).json({ user });
}
);
Every other example I can find seems to use different code after the passport.authenticate("local"
part. All I'm getting back in the client is a 401.
**Update: ** Adding client code where the login function is called if that helps.
export const login = (user) => {
return (
axios
// .post('/users/login', {
.post(baseURL + '/users/login', {
//.post(baseURL + '/auth/', {
username: user.email,
password: user.password,
rememberMe: user.rememberMe
})
.then((res) => {
...