2016-07-15 87 views
0

第一次使用護照,我意識到當我在策略回調中記錄數據時,它不會顯示在控制檯中,它是如何工作的,還是我做錯了什麼?Passport Not calling callback

passport.use(new GoogleStrategy({ 
    clientID: process.env.GOOGLE_CLIENT_ID, 
    clientSecret: process.env.GOOGLE_CLIENT_SECRET, 
    callbackURL: process.env.CALLBACK_URL, 
    passReqToCallback: true 
}, function (accessToken, refreshToken, profile, done) { 
    console.log('this should be displayed'); 
    done(profile) 
    } 
); 

路線是這樣的:

app.get('/api/v1/authenticate/google', passport.authenticate('google', { scope: ['https://www.googleapis.com/auth/plus.login'] })); 

回答

1

我有同樣的問題,並通過調用類似這樣的認證解決它:

passport.authenticate('facebook', { 
    scope: ['email', 'public_profile', 'user_likes', 'user_birthday'], 
    callbackURL: "http://localhost:1337" + req.url 
}, function (err, user) { 
    if (err) return res.negotiate(err); 
    // Do something 
})(req, res, next); 
-1

如果設置passReqToCallback:true,你的回調會被調用以req作爲第一個參數。

你應該叫它爲 function (req, accessToken, refreshToken, profile, done), 所以done將在正確的位置。編輯: 我意識到你的意思是主要回調。 這是一個兩步過程,因此process.env.CALLBACK_URL也應指向註冊護照中間件的路由。

從文檔:

如果callbackURL: "http://yourdormain:3000/auth/google/callback",

app.get('/auth/google/callback', 
    passport.authenticate('google', { 
     successRedirect: '/auth/google/success', 
     failureRedirect: '/auth/google/failure' 
})); 
相關問題