w*s 发帖数: 7227 | 1 我老做了网站的login, logout,好像可以。问题是logout以后按browser back key,
尼玛以前只能login后看的内容又全出来了。
用了这个但还不行
app.get('/logout', isLoggedIn, function(req, res) {
req.logOut();
if (!req.user)
res.header('Cache-Control', 'private, no-cache, no-store, must-
revalidate');
res.redirect('/login');
}); | w*s 发帖数: 7227 | 2 简化版的程序,logout, 按了back key后
isAuthenticated() 还说 "you are logged in".
var express = require('express');
var passport = require('passport');
var net = require('net');
var bodyParser = require('body-parser');
var http = require('http');
var multer = require('multer');
var cp = require('child_process');
var exec = require('child_process').exec;
var sys = require('sys');
var path = require('path');
var util = require('util');
var session = require('express-session');
var crypto = require('crypto');
var sqlite3 = require('sqlite3');
/////////////////////////////////////////////////
var LocalStrategy = require('passport-local').Strategy;
var db = new sqlite3.Database('./myPassword.db');
passport.use(new LocalStrategy(function(username, password, done)
{
console.log("step 2: Client sent you user: " + username + " password: "
+ password);
db.get('SELECT slat FROM users WHERE username = ?', username, function(
err, row)
{
if (!row) return done(null, false);
console.log("step 4");
db.get('SELECT username, id FROM users WHERE username = ? AND
password = ?',
username, password, function(err, row)
{
console.log("step 6");
if (!row) return done(null, false);
console.log("step 8");
return done(null, row);
});
});
}));
passport.serializeUser(function(user, done) {
return done(null, user.id);
});
passport.deserializeUser(function(id, done) {
db.get('SELECT id, username FROM users WHERE id = ?', id, function(err,
row)
{
if (!row)
return done(null, false);
return done(null, row);
});
});
/////////////////////////////////////////////////
var isAuthenticated = function(req, res, next)
{
//if (req.user.authenticated)
if (req.isAuthenticated()) {
console.log("Very good, you are logged in ...");
return next();
}
console.log("Sorry, you are NOT logged in yet ...");
res.send(200);
};
/////////////////////////////////////////////////
var app = express();
/////////////////////////////////////////////////
var server = http.createServer(app);
/////////////////////////////////////////////////
app.use(function(req, res, next) {
if (!req.user) {
console.log('Cannot display 1 ...');
res.header('Cache-Control', 'private, no-cache, no-store, must-
revalidate');
}
console.log('Cannot display 2 ...');
next();
});
app.use(express.static('../client/', {index: 'login.html'} ));
app.use(bodyParser());
app.use(session({ secret: 'my test cookie' }));
app.use(passport.initialize());
app.use(passport.session());
app.post('/auth/login', passport.authenticate('local',
{
successRedirect: '/index.html#/uploads',
failureRedirect: '/login.html',
}));
app.get('/auth/logout', function(req, res)
{
console.log("logging out ......");
req.session = null;
req.logout();
res.send(200);
});
app.get('/', isAuthenticated, function(req, res)
{
res.sendfile(path.resolve('../client/index.html'));
});
app.get('/systemStatus', isAuthenticated, function(req, res)
{
console.log("getting data from backend");
// skip details here ...
});
server.listen(5678); |
|