一切似乎都起作用。對Parse.com的請求將檢查用戶是否存在,並創建或檢索它。 我有兩個主要問題。首先,當調用deserializeUser
函數時,它將返回20個相同的用戶,並返回該函數,因爲它應該返回這20個用戶。 另外,我無法訪問req.user
Node.js Passport反序列化用戶返回20+個有效用戶
passport.serializeUser(function(user, done) {
done(null, user.objectId);
});
passport.deserializeUser(function(uid, done) {
console.log(uid)
// This outputs the ObjectId 20 TIMES!! ??
Parse.findUser(uid,function(error,user){
done(null, user);
})
});
// CONFIGURATION
app.configure(function() {
app.use(express.bodyParser()); //read
app.use(express.cookieParser()); //read
app.use(express.session({ secret: process.env.SESSION_SECRET || 'abcde' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(__dirname + '/static'))
});
passport.use(new FacebookStrategy({
clientID: process.env.FACEBOOK_APP_ID || 'app_id',
clientSecret: process.env.FACEBOOK_SECRET || 'fb_secret',
callbackURL: "http://localhost:5000/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, done) {
process.nextTick(function() {
_parse.user(accessToken, profile,function(parseUser){
//this returns the user.....
return done(null, parseUser);
})
})
})
)
app.get('/auth/facebook',
passport.authenticate('facebook',{scope:'email'}),
function(req, res){
// The request will be redirected to Facebook ....
});
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { failureRedirect: '/login' }),
function(req, res) {
console.log(req.user)
// this works! ...
res.redirect('/browse');
});
app.get('/browse',function(req,res){
console.log(req.user)
// this is works too ...
res.render('browse.jade',{title:'Browse',classes:'browse'})
})
app.get('/logout', function(req, res){
req.logout();
res.redirect('/login');
});
你在使用什麼解析庫?這看起來不像官方的Parse JavaScript SDK。 – bklimt
JavaScript的SDK是爲客戶端,我沒有嘗試過在服務器上,但我很確定它不起作用(有人糾正我,如果我錯了)...我使用[此模塊] (https://github.com/tenorviol/node-parse-api),它從Parse成功返回用戶。 – Maroshii
是否有任何特殊原因導致您未使用由Parse.com提供的官方JavaScript API http://parse.com/docs/downloads? –