在使用facebook passport

时间:2015-06-19 23:01:01

标签: javascript node.js facebook passport.js passport-facebook

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

流程应如下:

  1. facebook authenticate
  2. 用户对象存储在某个地方[我该怎么做?]
  3. 单击
  4. 链接,路由到post参数为link和userid的方法。 (如果链接存在,则将用户附加到我的linkschema中定义的用户数组中)
  5. / =====================================
    / / 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('/');
    });

    提前谢谢!

1 个答案:

答案 0 :(得分: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…': {…} , … }
}