使用 sapper 和 polka 的 Facebook 策略

Facebook strategy with sapper and polka

我需要与 polka(不是 Express)建立联系并使用 passport-facebook 策略。在我的服务器上:

var app = polka();
passport.serializeUser(function(user, cb){
    return cb(null, profile);
});

passport.deserializeUser(function(obj, cb){
    return cb(null, obj);
});

passport.use(new Strategy({
    clientID: *****,
    clientSecret: '******',
    callbackURL: 'http://localhost:3000/login/facebook/callback',
    //campo opzionale, specifico gli oggetti del profilo che voglio tornato
    profile: ['id', 'displayName']
  },
  function(accessToken, refreshToken, profile, done) {
    //Check the DB to find a User with the profile.id
    User.findOne({ uid: profile.id }, function(err, user) {
      if(err) {
        console.log(err);  // handle errors!
      }
      if (user) {
        console.log("find user!", user);
        done(null, user); //Login if User already exists
      } else { //else create a new User
        user = new User({
          uid: profile.id, //pass in the id and displayName params from Facebook
          name: profile.displayName
        });
        user.save(function(err) { //Save User if there are no errors else redirect to login.
          if(err) {
            console.log(err);  // handle errors!
          } else {
            console.log("saving user ...");
            done(null, user);
            console.log("user", user);
          }
        });
      }
    });
  }
));

app 
    //va sempre messo prima di passport.session
    .use(require('express-session')({ secret: 'keyboard cat', resave: true, saveUninitialized: true }))
    .use(passport.initialize())
    .use(passport.session())

    //definisco le rotte

    //rotta di login
    .get('/login/facebook',
        passport.authenticate('facebook')
    )

    //callback

    .get('/login/facebook/callback',

        //da confermare se è supportato il fialureRedirect
        //passport.authenticate('facebook', {failureRedirect: '/'}),
        passport.authenticate('facebook', {failureRedirect: '/'}),
        function(req, res){
            res.writeHead(302, {
                'Location': '/admin/menu',
                'content-type': 'document'
            });
            res.end();
        }
    )

    .use(
        compression({ threshold: 0 }),
        sirv('static', { dev }),
        formData.parse(options),
        formData.format(),
        //formData.stream(),
        formData.union(),
        json(),
        sapper.middleware()
    )

当我登录时,控制台还显示我已经登录(它还在 mongodbclient 上创建了一个对象,并且也找到了),但问题是在某些 url 上重定向了我

http://localhost:3000/login/facebook/callback?code=AQBBJIV61HxahPCtWBgMR23OMpU8MXfxDcD3BeRDTUw2rK5FvTnXq5n... 

有留言"profile is not defined"

在控制台上我有这个待处理

在 serializeUser 上,您 return 从未声明过的配置文件必须使用:

passport.serializeUser(function(user, cb){
   return cb(null, user);
});