Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

269
Vistas
Passport-Facebook not returning profile email

I'm trying to set up facebook authentication using passport from node.js. When I run the app and go to /auth/facebook, I go to a facebook page to give permission for facebook to return my email.Here's my code

Front-end.tsx

<a href='http://localhost:3001/auth/facebook'>
  <FacebookButton>Signup with Facebook</FacebookButton>
</a>

Backend.ts


    const { id, secret }: FACEBOOK_APP_SECRET = config.get('Facebook');
    const FacebookStrategy = passportFacebook.Strategy;
    passport.use(
      new FacebookStrategy(
        {
          clientID: id,
          clientSecret: secret,
          callbackURL: 'http://localhost:3001/auth/facebook/callback',
          profileFields: ['id', 'displayName', 'link', 'email'],
        },
        function (accessToken, refreshToken, profile, cb, done) {
          console.log(profile);
          console.log(profile.email);
          userModel.findOne({ facebook: profile.id }, (err: NativeError, existingUser: User) => {
            if (err) {
              console.log(err);
              return done(err);
            }
            if (existingUser) {
              console.log(existingUser);
              return done(undefined, existingUser);
            } else {
              userModel.findOne({ email: profile._json.email }, (err: NativeError, existingEmailUser: User) => {
                if (err) {
                  console.log(err);
                  return done(err);
                }
                if (existingEmailUser) {
                  done(err);
                } else {
                  const user: any = new userModel();
                  user.email = profile._json.email;
                  user.facebook = profile.id;
                  user.save((err: Error) => {
                    done(err, user);
                  });
                }
              });
            }
          });
        },
      ),
    );

Route.ts

this.router.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email', 'public_profile'] }));
this.router.get('/auth/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/login' }), function (req, res) {
      // Successful authentication, redirect home.
      res.redirect('/');
    });

So right now when I print out the profile , it will return the followings

{
  access_token: 'xxxxxxxxxxxxxxxxxxx',
  token_type: 'bearer',
  expires_in: 5181888
}

and with profile.email ,it will only return undefined

I search for a while , and other people are wrong at they did not attach { scope: ['email', 'public_profile'] } at their code which I did, so I don't know what is the problem here, why can't I return email in the profile ?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda