2015-06-19 67 views
0

我想在用不同的方法使用护照成功进行身份验证后,将用户对象保存在从facebook返回的请求对象中。这样做的目的是在我的linkschema中包含通过相同链接发布的所有不同用户。如何在使用Facebook护照进行身份验证后正确保存从我的req对象返回的用户对象

流应该如下:

  1. 认证的Facebook
  2. 用户对象存储在某处[如何做到这一点PART ???]
  3. 链接被点击时,得以被路由选择POST方法其参数是链接和用户标识。 (如果存在链接,用户被添加到用户的在我linkschema定义的数组)

/ ===================================== 
 
// FACEBOOK ROUTES 
 
// ===================================== 
 
// route for facebook authentication and login 
 
passport.use(new FacebookStrategy({ 
 
    clientID: configAuth.facebookAuth.clientID, 
 
    clientSecret: configAuth.facebookAuth.clientSecret, 
 
    callbackURL: "http://localhost:3000/auth/facebook/callback/" 
 
    }, 
 
    function(accessToken, refreshToken, profile, done) { 
 
    UserSchema.AddUnique(profile, accessToken, function(err, user) { 
 
     if (err) { 
 
     return done(err); 
 
     } 
 
     return done(null, user); 
 
    }); 
 
    } 
 
)); 
 

 
// Redirect the user to Facebook for authentication. When complete, 
 
// Facebook will redirect the user back to the application at 
 
//  /auth/facebook/callback 
 
router.get('/auth/facebook', passport.authenticate('facebook', { 
 
    scope: 'email' 
 
})); 
 

 
// Facebook will redirect the user to this URL after approval. Finish the 
 
// authentication process by attempting to obtain an access token. If 
 
// access was granted, the user will be logged in. Otherwise, 
 
// authentication has failed. 
 
var user = router.get('/auth/facebook/callback', 
 
    passport.authenticate('facebook', { 
 
    failureRedirect: '/login' 
 
    }), function(req, res) { 
 
    var user = req.user; 
 
    res.redirect('/browse'); 
 
    return function() { 
 
     return user; 
 
    }(); 
 
    }); 
 

 

 
function user() { 
 
    console.log('in here'); 
 
    console.log(user); 
 
} 
 

 
router.use(function(err, req, res, next) { 
 
    console.log(err) 
 
    next(err) 
 
}); 
 

 
router.get('/logout', function(req, res) { 
 
    req.logout(); 
 
    res.redirect('/'); 
 
});

预先感谢您!

回答

0

它们储存在外面的对象

var Users = {}; 


passport.use(new FacebookStrategy({…}, 
    function(accessToken, refreshToken, profile, done) { 
    UserSchema.AddUnique(profile, accessToken, function(err, user) { 
     if (err) { 
     return done(err); 
     } 

     // Push the user into that object 
     Users[user.id] = user; 

     return done(null, user); 
    }); 
    } 
)); 


function user() { 
    console.log(Users) //=> { '5234…': {…} , '5345…': {…} , … } 
} 
相关问题