0
votes

I have a react front-end hitting an express server that is only use for API calls. I'm using passport for authentication and I'm having some issues. I have a strategy set up for Facebook. I am able to login using Facebook, the serialize method is called, the user is added to the DB, and I'm able to see the req.user object in the strategy callback method but, I am not able to get the req.user object after this point. Any suggestions?

App.js

let express = require('express');
let router = express.Router();
let passport = require('passport');

router.get('/login/facebook', passport.authenticate('facebook', {scope: ["email"]}));

router.get('/facebook/return', 
    passport.authenticate('facebook', { failureRedirect: '/' }),
    (req, res) => {
        res.redirect('http://localhost:3000');
});

router.get('/logout', 
    passport.authenticate('facebook', { failureRedirect: '/' }),
    (req, res) => {
        req.logout();
        res.redirect('http://localhost:3000');
});


module.exports = router;

Auth.js

let express = require('express');
let router = express.Router();
let passport = require('passport');

router.get('/login/facebook', passport.authenticate('facebook', {scope: ["email"]}));

router.get('/facebook/return', 
    passport.authenticate('facebook', { failureRedirect: '/' }),
    (req, res) => {
        res.redirect('http://localhost:3000');
});

router.get('/logout', 
    passport.authenticate('facebook', { failureRedirect: '/' }),
    (req, res) => {
        req.logout();
        res.redirect('http://localhost:3000');
});


module.exports = router;

Species.js

let express = require("express");
let router = express.Router();
let Species = require("../models/species").Species;

router.get('/', 
(req, res, next) => {
    console.log(req.user);
    Species.find({}).exec((err, species) => {
        if(err) return next(err);
        res.json(species);
    });
});

module.exports = router;
1
I have great suggestion. Check How to Ask first. Especially minimal reproducible example part.bennygenel
We would need to see your code to provide assistance.Kyle Richardson

1 Answers

0
votes

Quick little tip from me

exports.fbauth = passport.authenticate('facebook', {scope: ['email']});
exports.fbcallback = passport.authenticate('facebook', {
    scope: ['email'],
    failureRedirect: '/login',
    failureFlash: 'Failed Login!',
    successRedirect: '/account',
    successFlash: 'You are now logged in!'
});

Make sure you include scope: email in both authentications