I’ve been having this issue for a week now. I feel like I’ve wasted so much time.
I’ll tell you the issue, then what I’ve done at the end.
Right Now I’ve got both Ends on custom Domains, with Heroku hosting.
Passport Local Logs me in
2022-02-17T06:39:06.949016+00:00 heroku[router]: at=info method=OPTIONS path="/login" host=www.thehoodconservative.com request_id=ff2dc525-7feb-4a2b-afe8-41ca7cc9610e fwd="172.58.196.240" dyno=web.1 connect=0ms service=4ms status=204 bytes=369 protocol=http
2022-02-17T06:39:07.069432+00:00 app[web.1]: Logged in
From this Code
passport.use(
new LocalStrategy(customFields, (username, password, done) => {
User.findOne({ username: username })
.then((user) => {
if (!user) {
console.log("No user");
return done(null, false);
} else {
const isValid = validPassword(password, user.hash, user.salt);
if (isValid) {
console.log("Logged in");
return done(null, user);
} else {
console.log("Wrong Password");
return done(null, true);
}
}
})
.catch((err) => {
done(err);
});
})
);
Yet I get this Response in the console
{auth: false, msg: 'Here'}
from this code on my backend
module.exports.isAuth = (req, res, next) => {
if (req.isAuthenticated()) {
next();
// res.status(200).json({ user: req.user, auth: true });
} else {
return res.status(401).json({ auth: false, msg: "Here" });
}
};
I have this in my Cookie storage
deviceId D0E8508F-7685-4C9D-AFB6-522EE27A3B93 localhost / Session 44
but the session in my DB doesnt show that number, it shows.
_id
:
"g3NYIQ7usEIJ5K8ETP1S-mDZImKy4Xj7"
expires
:
2022-02-18T06:50:34.597+00:00
session
:
"{"cookie":{"originalMaxAge":86400000,"expires":"2022-02-18T06:50:34.59..."
My history of aggravation
I started with Having my BE (Backend) on heroku and FE on Netlify.
Some guy on here told me its for some reasons its better to have my FE on Heroku along with BE so I did. Still same result. After hours of fussing around and changing random things and going through SOF, I read through what I saw was issues with Heroku not being able to set Cookies, somethoing about Public Suffix list. So Through a bunch of research I came to the conclusion I need to host my BE and FE on custom domains. Which I’ve done now. Yet now its the same exact issue.
I have a feeling I need lessons on debugging Cookies, but I have no idea how to do that. When I had this local, both FE and BE, It worked.
I would love it if someone showed me a better place than Heroku to host and make this work. I dont care what I have to do to get this to work.
I’m also a newbie, this is my first full stack project, without a tutorial, so I’m not sure what I need to share and dont want to spam this with too much code, just comment what you need to see, and I’ll edit this post.