I have checked a similar thread here where PRAKASH THOMAS VARGHESE broke tdown the process of the session quite thoroughly, but I am afraid I have found myself in a different problem.
up until the final steps, user is visiable in my console.logs and when it's routed to my callback url, req.isAuthenticated is true and req.user works. From the final step I res.redirect to my homepage in react...
app.get(
"/auth/google/login",
passport.authenticate("google", {
failureRedirect: "http://http://localhost:3000/old/all"
}),
(req, res) => {
console.log(`Authenticated: ${req.isAuthenticated()}`); //READS TRUE
console.log(`user: ${req.user}`); // USER SHOWS UP WITH ALL PARTS SHOWING
res.redirect("http://localhost:3000/");
}
);
When redirected to home (in a react app) I have a fetch request sent checking if user isAuthenticated in another route.
axios
.get("http://localhost:5000/getuser")
.then(res => {
setUser(res.data);
console.log(res.data);
})
.catch(err => {
console.log(err);
});
}, []);
over at /getuser....
app.get("/getuser", (req, res) => {
console.log("user:", req.user); // READS UNDEFINED HERE
console.log("/getuser isAuthenticated:", req.isAuthenticated()); //READS AS FALSE HERE
if (req.isAuthenticated()) {
const user = req.user;
res.json({ user: user, isAuthenticated: true });
} else {
const user = false;
res.json({ user: false, isAuthenticated: false });
}
});
For whatever reason I have not been able to determine yet the req.user becomes undefined and isAuthenticated reads false. My database saves the user and the sessions, and i dont know what happens from there.
The only other lead I have to go on is pasport.deserializeUser seems to run twice but I see nothing in the code that would cause this.
const sessionStore = new MongoStore({
//url: process.env.MONGOAUTH,
url: "mongodb://localhost:27017/friftr",
ttl: 1 * 24 * 60 * 60, // = 1 days. Default
autoReconnect: true
}).on("error", function(err) {
if (err.errno === "EADDRINUSE") {
console.log("port busy 33");
console.log(err);
} else {
console.log(err);
}
});
// Express Session
const sessionMiddleware = session({
secret: process.env.SECRET2,
resave: true,
store: sessionStore,
saveUninitialized: true
});
app.use(sessionMiddleware); // 1
//passport middleware
app.use(passport.initialize()); //2
app.use(passport.session()); //3
//ONCE MONGOOSE IS SETUP WE CAN BEGIN SIGNING IN
userSchema.plugin(passportLocalMongoose); // MUST BE BEFORE const User
userSchema.plugin(findOrCreate);
const User = mongoose.model("User", userSchema); // MUST BE AFTER userSchema.plugins(passportLocalMongoose)
passport.use(User.createStrategy());
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
console.log(`user here:`, user);
done(err, user);
});
});
passport.use(
new GoogleStrategy(
{
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: "/auth/google/login"
},
(accessToken, refreshToken, profile, cb) => {
console.log(`first`, cb);
User.findOrCreate(
{
googleId: profile.id,
email: profile._json.email,
username: profile._json.email
},
(err, user) => cb(err, user)
);
}
)
);
app.get(
"/auth/google",
passport.authenticate("google", { scope: ["profile", "email"] })
);
app.get(
"/auth/google/login",
passport.authenticate("google", {
failureRedirect: "http://http://localhost:3000/old/all"
}),
(req, res) => {
console.log(`Authenticated: ${req.isAuthenticated()}`);
console.log(`user: ${req.user}`);
res.redirect("http://localhost:3000/");
}
);
app.get("/logout", function(req, res) {
req.logout();
res.redirect("http://localhost:3000/");
});