2012-10-04 24 views
0

一切似乎都起作用。對Parse.com的請求將檢查用戶是否存在,並創建或檢索它。 我有兩個主要問題。首先,當調用deserializeUser函數時,它將返回20個相同的用戶,並返回該函數,因爲它應該返回這20個用戶。 另外,我無法訪問req.userNode.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'); 
    }); 
+0

你在使用什麼解析庫?這看起來不像官方的Parse JavaScript SDK。 – bklimt

+0

JavaScript的SDK是爲客戶端,我沒有嘗試過在服務器上,但我很確定它不起作用(有人糾正我,如果我錯了)...我使用[此模塊] (https://github.com/tenorviol/node-parse-api),它從Parse成功返回用戶。 – Maroshii

+0

是否有任何特殊原因導致您未使用由Parse.com提供的官方JavaScript API http://parse.com/docs/downloads? –

回答

8

我懷疑是您加載一個頁面,20個不同的資源,並且每次通話是單個請求的資源之一。

app.use(express.static(__dirname + '/static'))中間件移動到中間件堆棧的頂部。由於這些資源是靜態的和公開的,因此它不需要分析正文,cookies或加載會話。

+0

謝謝!真棒,它的作品!那麼,我到底做了什麼?爲什麼訂單重要? – Maroshii

+1

請求將按照它們列出的順序通過中間件,直到其中一個發送響應。靜態中間件立即響應,跳過認證等任何動態行爲。 –

+0

這節省了我幾個小時的頭痛 - 謝謝! – 828