如何在快速会话中清除浏览器的sid cookie?

时间:2018-02-13 18:59:07

标签: express session cookies passport.js

在我的express js app上,我设置了一个会话cookie,但即使在注销后,我的cookie也不会从浏览器中删除。因此,单击登录,我可以在没有有效身份验证的情况下重新登录。

这是我的app.js

'use strict';

var auth = require('http-auth');
var bodyParser = require('body-parser');
var config = require('./test/lib/utils/config');
var cookieParser = require('cookie-parser');
var express = require('express');
var mysql = require('mysql2');
var passport = require('passport');
var path = require('path');
var session = require('express-session');
var favicon  = require('serve-favicon');

var Auth = require('./lib/auth');
var Utils = require('./lib/utils');

var admin = require('./routes/admin');
var committee = require('./routes/committee');
var index = require('./routes/index');
var logout = require('./routes/logout');
var professor = require('./routes/professor');
var roles = require('./routes/roles');

var creds = config.credentials.database;
var connection = mysql.createConnection(creds);
connection.connect();
var authentication = new Auth(connection);
var utils = new Utils(connection);

var basic = auth.basic({
    realm: 'Welcome to My App',
    file: path.resolve(__dirname, '.private', '.htpasswd')
}, function(username, password, cb) {
    utils.getMemberId(username, function(err, id) {
        if (err) return cb(err);
        utils.isLoggedIn(id, function(err, isLoggedIn) {
            if (err) return cb(err);    
            if (!isLoggedIn) {
                authentication.logIn(id, function(err) {
                    if (err) return cb(err);
                    return cb(id);
                });
            } else {
                return cb(id);
            }
        });
    });
});

var app = express();
passport.use(auth.passport(basic));

// Setup strategy. 
passport.serializeUser(function(user, done) {
    done(null, user);
});

passport.deserializeUser(function(user, done) {
    done(null, user);
});

app.use(favicon(path.join(__dirname, 'public', 'image', 'favicon.ico')));
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({ name: 'sid', rolling: true, secret: 'keyboard cat', cookie: { maxAge: 30000, httpOnly: true }}));
app.use(passport.initialize());
app.use(passport.session());

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use('/', index);
app.use('/roles', [passport.authenticate('http', {session: true}), setUserId, 
    setUserRoles, setUserFirstName, setUserFullName], roles);

app.use('/roles/admin', admin);
app.use('/roles/committee', committee);
app.use('/roles/professor', professor);
app.use('/logout', performLogout, logout);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handler
app.use(function(err, req, res) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};

    // render the error page
    res.status(err.status || 500);
    res.render('error');
});

function setUserId(req, res, next) {
    console.log(JSON.stringify(req.session));
    utils.getMemberId(req.session.passport.user, function(err, id) {
        if (err) next(err);
        req.session.passport.id = id;
        next();
    });
}

function setUserRoles(req, res, next) {
    utils.getRoles(req.session.passport.id, function(err, roles) {
        if (err) next(err);
        req.session.passport.roles = roles;
        next();
    });
}

function setUserFullName(req, res, next) {
    utils.getMemberFullName(req.session.passport.id, function(err, fname) {
        if (err) next(err);
        req.session.passport.fullname = fname;
        next();
    });
}

function setUserFirstName(req, res, next) {
    utils.getMemberFirstName(req.session.passport.id, function(err, fname) {
        if (err) next(err);
        req.session.passport.fname = fname;
        next();
    });
}

function performLogout(req, res, next) {
    authentication.logOut(req.session.passport.id, function(err) {
        if (err) next(err);
        next();
    });
}

module.exports = app;

这是我的logout.js路由器:

 'use strict';

var express = require('express');
var router = express.Router();

router.get('*', function(req, res) {
    console.log('Session before logging out: ' + JSON.stringify(req.session));
    req.session.destroy(function() {
        res.clearCookie('connect.sid', { name: 'sid', rolling: true, secret: 'keyboard cat', cookie: { maxAge: 30000, httpOnly: true }});
        res.redirect('/');
        console.log('Session after logging out: ' + JSON.stringify(req.session));
    });
});

module.exports = router;

我不确定我做错了什么不能正确清除会话cookie。任何有关立即指导的帮助都将受到赞赏。

0 个答案:

没有答案