How can I access profile information in google Oauth2 passport?

451 views Asked by At

So I want the index page to show the name of the user currently logged in in the footer of the index page.
My google login routes

router.get("/login/google", passport.authenticate('google', { scope:
    [ 'email', 'profile' ] }));
router.get("/login/google/callback", passport.authenticate("google", {
    successRedirect: "/videos",
    failureRedirect: "/login/google/error"
}));
router.get("/login/google/error", (req, res) => {
    res.send("Error in google login")
})

My google Strategy

passport.use(new GoogleStrategy({
    clientID: GOOGLE_CLIENT_ID,
    clientSecret: GOOGLE_CLIENT_SECRET,
    callbackURL: "http://localhost:3000/login/google/callback",
    passReqToCallback: true
  },
  function(request, accessToken, refreshToken, profile, done) {
    User.findOrCreate({ googleId: profile.id }, function (err, user) {
      return done(err, user);
    });
  }
));

My footer of the index file

    <footer>
    <% if(!auth){ %>
      <li class="nav-item">
        <a href="/register" class="nav-link active">Register!</a>
        <li class="nav-item">
          <a href="/login" class="nav-link active">Log In</a>
        </li>
      </li>
    <% } else { %>
      <li class="nav-item">
        <a href="#" class="nav-link disabled">Welcome, <%= auth.username %> </a>
      </li>
      <li class="nav-item">
        <a href="/logout" class="nav-link active">Log Out</a>
      </li>
    <% } %>
  <div class="d-flex justify-content-center mt-3">
  <ul class="nav">
    <li class="nav-item">
      <div class="g-ytsubscribe" data-channelid="UCAjHeCmPJ5lDVWw6dL_jQng" data-layout="default" data-count="default"></div>
    </li>
  </ul>
</footer>

My Footer
Footer IMG

1

There are 1 answers

0
Someone21 On BEST ANSWER

I setted user to profile in my google strategy and then idk how it was saved in req.user
EDIT:
In other words I saved the profile variable in another variable (req.user).