2016-09-29 2 views
0

MEAN 스택 응용 프로그램에서 페이스 북 인증을 설정하려고합니다. 서면 코드는 페이스 북에서 사용자를 인증 할 때까지 작동하지만, 애플리케이션으로 되돌려 보내면 콜백 기능이 실행되지만 passport.authenticate('facebook'..... 부분은 실행되지 않습니다.Node.js를 사용하여 passport.js로 성공적인 인증 콜백을 설정하려면 어떻게해야합니까?

이것은 루트 파일에 내 코드입니다 :

app.get('/auth/facebook', passport.authenticate('facebook', { scope : 'email' })); 

    app.get('/auth/facebook/callback', function() { 
     console.log('callback') 
     passport.authenticate('facebook', { 
      successRedirect : '/', 
      failureRedirect : '/fail' 
      }) 
    }); 

"콜백"콘솔에 출력되지만 다음 응용 프로그램은로드의 상태 그 때까지 시간 초과입니다. 당신의 /auth/facebook/callback 당신이 당신의 경로 처리기 내부가 아닌 즉 미들웨어로 passport.authenticate을 실행중인에서는

// config/passport.js 



// load all the things we need 
var LocalStrategy = require('passport-local').Strategy; 
var FacebookStrategy = require('passport-facebook').Strategy; 

// load up the user model 
var User  = require('../app/models/user'); 

// load the auth variables 
var configAuth = require('./auth'); 


module.exports = function(passport) { 


    passport.use('facebook', new FacebookStrategy({ 
     clientID  : configAuth.facebookAuth.clientID, 
     clientSecret : configAuth.facebookAuth.clientSecret, 
     callbackURL  : configAuth.facebookAuth.callbackURL, 
     profileFields: ["emails", "displayName"] 
    }, 

     // facebook will send back the tokens and profile 
     function(access_token, refresh_token, profile, done) { 
     // asynchronous 
     process.nextTick(function() { 

      // find the user in the database based on their facebook id 
      User.findOne({ 'id' : profile.id }, function(err, user) { 

      // if there is an error, stop everything and return that 
      // ie an error connecting to the database 
      if (err) 
       return done(err); 

       // if the user is found, then log them in 
       if (user) { 
       return done(null, user); // user found, return that user 
       } else { 
       // if there is no user found with that facebook id, create them 
       var newUser = new User(); 

       // set all of the facebook information in our user model 
       newUser.fb.id = profile.id; // set the users facebook id     
       newUser.fb.access_token = access_token; // we will save the token that facebook provides to the user      
       newUser.fb.firstName = profile.name.givenName; 
       newUser.fb.lastName = profile.name.familyName; // look at the passport user profile to see how names are returned 
       newUser.fb.email = profile.emails[0].value; // facebook can return multiple emails so we'll take the first 

       // save our user to the database 
       newUser.save(function(err) { 
        if (err) 
        throw err; 

        // if successful, return the new user 
        return done(null, newUser); 
       }); 
      } 
      }); 
     }); 
    })); 


} 
+0

는 여권 파일에 일부 로그를 추가 하시겠습니까? 어디로 가는지 봐? – DrakaSAN

+0

@DrakaSAN 여권 파일을 전혀 실행하지 않는 것 같습니다. 나는 여권 파일의 다양한 부분에 console.log (x)를 추가했으며 전혀 실행하지 않습니다. – James

답변

0

:

이것은 내가 여권을 구성하는 데 사용되는 passport.js 파일입니다. 문서에서 custom callback이라고하는 것. 이 호출은 함수를 반환하지만 아무것도 실행하지 않도록 실행하지 않습니다.

사용 실제로 ( the usual way입니다) 미들웨어 여기 passport.authenticate을 실행하거나 중 하나가 필요합니다

req, resnext 인수 경로 처리기가 수신 (하지만 당신은 생략)와 내 authenticate 호출에 전달 라우트 핸들러.

미들웨어 방식은 documentation of passport-facebook에 표시됩니다. 당신을 위해 그것을는 다음과 같습니다 어떤 이유로 사용자 정의 콜백을 사용하고자 할 경우,이 아래와 같은 뭔가가 필요 것

app.get('/auth/facebook/callback', 
    passport.authenticate('facebook', { 
     successRedirect : '/', 
     failureRedirect : '/fail' 
    }) 
); 

:

app.get('/auth/facebook/callback', function(req, res, next) { 
    passport.authenticate('facebook', function(err, user, info) { 
     // Do your things and then call `req.logIn` and stuff 
    })(req, res, next); 
}); 
관련 문제