护照 isAuthenticated() 总是 returns TRUE

Passport isAuthenticated() always returns TRUE

使用护照本地身份验证,登录有效,我单击 getStatus 按钮有效,然后注销有效。但是注销后,我在浏览器中点击BACK,仍然可以显示getStatus的全部内容。控制台登录 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("asking for Json data from backend");
    // skip details here ...
});

server.listen(5678);

查看护照索引时。 app.use(passport.initialize()) 的使用是在每条路线上初始化一个空白用户。由于上面的代码是在主 app.js 文件上使用的,而不是在特定的路由中使用的,所以每次向您的服务器发出请求时都会执行它,即使有人没有登录,也基本上会创建一个空白用户。下面link是护照号码

https://github.com/jaredhanson/passport/blob/master/lib/authenticator.js

关于为什么护照配置不正确以达到预期效果的讨论,以及为什么我值得你虚构的互联网点数。在讨论申请时,我必须确保我们在同一页上。

对于讨论,我将引用使用以下文件结构生成的应用程序:$ npm install -g express-generator

(其实还有更多的文件,但是你可以在快递网站上查看。)

myProject
  |___bin
    |___www.js       //server.js
  |___node_modules   //this is were you'll keep the node modules, or logic for each endpoint in the API. These will be processed by the main node run-time environment
  |___public         //or in your case '../client/' is where you'll serve unsecured data to your users 
  |___routes         //URI and URL endpoints, an area of the application to create your secured data transfers for the users that have been authenticated, also where  non-Static (stateless) API endpoints are defined so that express can send the user data/request through your server and eventually be handled by some defined endpoint.
    |___index.js
  |___views          //if using a view engine
  |___app.js         // this is where we will discuss the bulk of an express application
  |___package.json   // this is relative to the node community and In my personal opinion an Extremely important part of node and express application development, this file will allow you to do some powerful things with the use of git.

app.js 是您的APP主持,它被称为Express Application。有些应用程序比其他应用程序更复杂,一个简单的应用程序可以是几个端点(A.K.A。URI 和 URLs)。如果它是一个简单的应用程序,可以将 API(应用程序接口)保留在称为 app.js 的主文件中。在更复杂的应用程序中,您将为文件命名,对于此示例,我将引用文件名 oAuth.js 来表示声明护照身份验证方法所表示的。

根据我使用 Web 应用程序的经验,您会有一个登录页面,可以是主页、登录名或某种新闻(通常在静态文件夹中定义为 index.html ) 在您的情况下,您将静态文件夹定义为 '../client/' 并传递对象 index.html。

在最新版本的 Express 4.X 中,静态文件服务是按照以下规定的方式完成的。

Serving files, such as images, CSS, JavaScript and other static files is accomplished with the help of a built-in middleware in Express - express.static.

Pass the name of the directory, which is to be marked as the location of static assets, to the express.static middleware to start serving the files directly. For example, if you keep your images, CSS, and JavaScript files in a directory named public, you can do this:

Express生成器会创建如下app.js文件,这是一个非常重要的配置方式。这第一部分有一些非常有用的节点模块,它们不像 express 那样固执己见,最终你将在其中导入一些你自己的节点 APIs

var express = require('express'),
path = require('path'), //core node module
logger = require('morgan'), //allows you to see the console.logs during development and see incoming and outgoing messages to the server. It also displays `console.log()` defined in any node_module, express route, and express app file.
cookieParser = require('cookie-parser'), //helps when trying to handle cookies
bodyParser = require('body-parser');   //helps when parsing certain types of data 

路由就像迷你快递应用程序,还记得我们第一次讨论某些应用程序如何变得比其他应用程序更复杂吗?这就是您管理复杂性的方式,以便您的应用程序能够成长和繁荣。假设您希望为喜爱的用户添加新功能。

var route = require('.routes/index',// this is importing the the logic of the URI and URL enpoint to this file. It will be referenced as the second argument in the route and configuration references below.
    oAuth = require('.routes/oauth') // this is the file where you'll place all of the passport logic you have previously wrote. Any other authenticated routes need to be defined in this file.

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade'); //alot of people are also using EJS equally if not more

现在设置 express-generator 提供的基本中间件

app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));  //this style  declaration ensures you hit what ever directory you want to by changing public as long as that directory exists. 

路由 URI 和 URL 端点由您定义到 -->express。它采用字符串的形式,并在此文件顶部对其路径进行引用

app.use('/', routes); //unsecured landing page 

此 express 对象用法将允许您定义 APIs、授权和未授权的路由。您将在此处声明对快速应用程序的已验证部分的引用。上面声明的应用程序的任何部分 app.use('/auth/',oAuth) 都不会被验证。在您的 URI 和 URL 的 /auth/ 部分下方声明的任何部分。将被认证。

app.use('/auth/', oAuth);  

express 生成器将放置在应用程序文件中的一些额外内容,这些内容非常有用。

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

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});

因为您正在用不必要的复杂性污染您的应用程序命名空间,所以它无意中导致了您不希望的身份验证影响。这更深入,因为它与格式化和配置您的应用程序接口以及 javascript 文件如何在 Node 运行-time 环境中执行,以及如何使用 express 应用程序框架有关在构建需要身份验证才能访问的复杂应用程序时配置。

现在回到您的问题,即为什么在没有人登录的情况下您仍然获得经过身份验证的用户?这是因为你使用了app.use('some string or some middleware')。要解决此问题,请删除所有身份验证处理并将其移至路由。在上面的示例中,它被引用为 oAuth.js。定义任何需要在护照中间件后面进行身份验证的路由。

现在因为你的问题也是关于节点的,你在评论中提到你是 scrum it's important to state that all of this information is contained on the express website 的一部分,这是我最初 link 编辑我的答案的地方。尽管我告诉你你需要一条路线并且护照配置不正确。所以任何煽动性的评论 "read the manual" 都是因为我觉得你甚至没有调查我在我最初的回答中发送的 link,你也没有阅读 express 框架工作和他们网站的任何其他部分。如果你打算了解任何 node_modules 和复杂的框架工作,那么阅读它们和做他们的教程同样重要,当有入门 and/or 时,实际上要通读 node_modules 他们有 API 参考资料。通过在不尝试框架基础知识的任何部分的情况下进行应用程序开发,只会让您花费更多时间编写糟糕的易破解代码。如果您不了解 node_modules 的功能,它将显着减慢您的开发速度。了解它们功能的最好方法是阅读它们。这就是我 rant/advice 学习如何开发 Web 应用程序的全部内容。最终,您将能够在应用程序中重用大量教程代码。