Passport JS和restify以及客户会话

时间:2015-04-25 19:44:35

标签: node.js session passport.js restify

我在护照上遇到了错误。我正在构建一个使用restify的api。我正在使用客户端会话进行会话和护照与谷歌oauth 2。

我遇到麻烦,它将序列化用户并创建会话。我似乎没有得到任何错误,但它将大型节点对象打印到控制台。

var restify = require('restify');
var massive = require('massive');
var passport = require('passport');
var GoogleStrategy = require('passport-google-oauth2').Strategy;
var _ = require('lodash');
var sessions = require("client-sessions");


var app = function(config, done) {

// declare DB and server

var server = restify.createServer(),
    db;


// Set server settings

server.use(restify.bodyParser());
server.pre(restify.pre.sanitizePath());
server.use(restify.queryParser());
server.use(sessions({
    // cookie name dictates the key name added to the request object
    cookieName: 'Skyrail_session',
    // should be a large unguessable string
    secret: 'abc123yyighhcggfgucgdguhvgcydtfugjvhfguijkvhgcfgvcfg',
    // how long the session will stay valid in ms
    duration: 24 * 60 * 60 * 1000,
    activeDuration: 1000 * 60 * 5
}));
server.use(passport.initialize());
server.use(passport.session());

server.use(function logger(req, res, next) {
    console.log(new Date(), req.method, req.url);
    next();
});

server.on('uncaughtException', function(request, response, route, error) {
    console.error(error.stack);
    response.send(error);
});




// passport auth settings

passport.use(new GoogleStrategy({
        clientID: ID,
        clientSecret: SECRET,
        callbackURL: 'http://127.0.0.1:8080/oauth2callback',
        passReqToCallback: true
    },
    function(request, accessToken, refreshToken, profile, done) {

        var isEmail = false;

        for (var i = profile.emails.length - 1; i >= 0; i--) {
            if (_.endsWith(profile.emails[i].value, 'test.co.uk')) {
                isEmail = true;
            }
        };

        if (isEmail) {
            // do user stuff
            done(null, profile);

        } else {

            done(null, profile);
        }

    }
));

passport.serializeUser(function(user, done) {
    console.log('serializing user.');

    done(null, user.id);
});

passport.deserializeUser(function(user, done) {
    console.log('deserialize user.');
    done(null, user.id);
});


// connect to the database

massive.connect({
    connectionString: config.postgres.conString
}, function(err, massiveInstance) {
    db = massiveInstance;
    done();
});


var google = passport.authenticate('google', {
    scope: 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'
});


var googleCallback = passport.authenticate('google');


var authenticate = passport.authenticate('google', {
    successRedirect: '/success',
    failureRedirect: '/fail'
})


// setup shceduler

var scheduler = require('node-schedule');


// return the appropriate method and objects.

return {

    server: server,
    db: db,
    authenticate: authenticate,
    google: google,
    googleCallback: googleCallback,
    scheduler: scheduler
}



};
   module.exports = app

这一切都很好,我最终得到谷歌接受,然后到达这一点。

passport.serializeUser(function(user, done) {
    console.log('serializing user.');

    done(null, user.id);
});

它会记录到控制台,然后开始丢弃节点对象。我很确定这与设置烹饪有关。

我对如何调试感到有点失落。关于使用护照和解决问题的信息不多。

1 个答案:

答案 0 :(得分:0)

旧问题,但请参见我的答案here,以获取有关如何将client-session与Passport结合使用的示例。

TL; DR:设置{cookieName: 'session'}(而不是“ Skyrail_session”)并序列化 entire 用户对象,而不仅仅是ID。如果Passport不能帮您这样做,则可能需要使用JSON.stringify()JSON.parse()