0

在使用护照成功认证后,我想将用户对象保存在从 facebook 返回的请求对象中,以便在不同的方法中使用。这样做的目的是在我的链接模式中包含通过同一链接发布的所有不同用户。

流程应如下所示:

  1. 脸书认证
  2. 用户对象存储在某处[我该如何做这部分???]
  3. 链接被点击,被路由到 post 方法,其参数是链接和用户 ID。(如果链接存在,则将用户附加到我的链接架构中定义的用户数组中)

/ =====================================
/ / 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('/');
});

先感谢您!

4

1 回答 1

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…': {…} , … }
}
于 2015-06-20T01:15:15.190 回答