2017-09-14 52 views
0

我有一個響應前端觸擊僅用於API調用的快速服務器。我正在使用護照進行身份驗證,並遇到一些問題。我有一個爲Facebook設立的戰略。我可以使用Facebook登錄,調用序列化方法,將用戶添加到數據庫,並且我能夠在策略回調方法中看到req.user對象,但是我無法獲取請求。用戶對象在此之後。有什麼建議麼?PassportJs用戶未定義React前端Express/MongoDB後端

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; 
+0

我有很好的建議。先檢查[問]。特別是[mcve]的一部分。 – bennygenel

+0

我們需要查看您的代碼才能提供幫助。 –

回答

0

快一點從我

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!' 
}); 

請確保您包括範圍:在兩個身份驗證電子郵件