• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

isAuthenticated() returns false after successful redirect

April 10, 2020 by

Questions › isAuthenticated() returns false after successful redirect
0
Vote Up
Vote Down
Garmaine asked 4 years ago

I have been stuck on this problem now for several days. From what I can conclude after leaving the backend port:5000 and redirecting to port:3000 (my frontend's homepage) the authentication session stops.

What I am doing: clicking a sign in to google button, login with good account, redirect to my front end homepage where a fetch request runs and gets the logged in user profile information.

I am running a sign in with google oAuth with passport.js. Mongoose saves the session and user profile info. req.isAuthenticated() should equal true on all routes until logged out. instead Everything works until the very last point where I res.redirect to the front end home page.

app.get(
  "/auth/google",
  passport.authenticate("google", { scope: ["profile", "email"] })
);

app.get(
  "/auth/google/login",
  passport.authenticate("google", { failureRedirect: "/new" }),
  (req, res) => {
    console.log(` Authentication Status: ${req.isAuthenticated()}`); // READS AS TRUE
    console.log(` User? : ${req.user}`); /// SHOWS USER INFO
    console.log("end of user status before redirect.");
    res.redirect("http://localhost:3000/");
  }
);

THE PROBLEM:

On the last function from what I can tell everything works as it is supposed to right up until the res.redirect. unless I put in the command req.logout() It should be able to keep track of my session and on the backend req.isAuthenticated should equal true. Instead in other routes where I have conditionals setup req.isAuthenticated is reading false.

// quick fetch request I run on front end to show user as logged in and profile info.
app.get("/getuser", (req, res) => {
  console.log("user CHECK :", req.user); // READS UNDEFINED HERE
  console.log(`is authenticated:`, req.isAuthenticated()); // READS FALSE HERE
  if (req.isAuthenticated()) {
    const user = req.user;
    console.log("user:", req.user);
    res.json({ user: user, isAuthenticated: true });
  } else {
    const user = false;
    console.log("user:", req.user);
    res.json({ user: false, isAuthenticated: false });
  }
});

It gets stranger…

My browser receives the connect.sid cookie and my database saves the user and session.

from chrome browser cookies on localhost: from chrome browser cookies on localhost

screenshot of saved account created from session in my database screenshot of saved account created from session in my database

I have been stuck on this problem for more than three days and have checked quite a few stack threads already on this. All of them did not seem to help me. Any help would be greatly appreciated. Thank you so much in advance and I will do my best to explain anything confusing.

Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: authentication, javascript, oauth-2.0, passport.js, session-cookies

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Copyright © 2023