护照本地路线

时间:2018-03-28 15:05:07

标签: node.js express routes passport-local

我正在尝试使用本地护照来限制访问网站 为此,我使用login_app运行passport-local,但这本身就是从主app.js调用的路由。 在尝试在第二级(护照文件/路线)上路由时,我发现我的代码已找到,但内部的功能未被调用。
这是我的login_app代码:

var express       = require('express');
var router        = express.Router();
var mongoose      = require ('mongoose');
var flash         = require('connect-flash');
var passport      = require('passport');
var LocalStrategy = require('passport-local').Strategy;

var morgan        = require('morgan');
var cookieParser  = require('cookie-parser');
var bodyParser    = require('body-parser');
var session       = require('express-session');

let app = express();

// pass passport for configuration
require('./passport-files/passport')(passport); 

//For express application
app.use(morgan('dev'));
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser()); // get information from html forms

//Initialize passport session
app.use(session({ secret: 'secret' })); // session secret
app.use(passport.initialize());
app.use(passport.session());
app.use(flash()); // use connect-flash for flash messages stored in session


// load our routes and pass in our app and fully configured passport
require('./passport-files/routes')(app, passport);

module.exports = router;

为了简化问题,我只将routes.js文件放在这里:

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

let app = express();
module.exports = function (app, passport) {
//function views (app, passport) {

/* GET users listing. */
// HOME PAGE (with login links) ========
app.get('/', function(req, res) {
    res.render('login-welcome', {});
});

// LOGIN ===============================
// show the login form
app.get('/log-in', function(req, res) {
    res.render('login', { message: req.flash('loginMessage') }); 
});

// process the login form
app.post('/log-in', passport.authenticate('local-login', {
    successRedirect : '/admin', // redirect to the secure profile section
    failureRedirect : '/log-in', // redirect back to the signup page if there is an error
    failureFlash : true // allow flash messages
}));

// we will use route middleware to verify this (the isLoggedIn function)
app.get('/profile', isLoggedIn, function(req, res) {
    res.render('profile', {
        user : req.user // get the user out of session and pass to template
    });
});

app.get('/logout', function(req, res) {
    req.logout();
    res.redirect('/');
}); 
};

// route middleware to make sure a user is logged in
function isLoggedIn(req, res, next) {

// if user is authenticated in the session, carry on 
if (req.isAuthenticated())
    return next();

// if they aren't redirect them to the hosme page
res.redirect('/');   

};

我几乎使用scotch.io(https://scotch.io/tutorials/easy-node-authentication-setup-and-local)中的示例构建了这个,但是我无法以这种方式工作。 如果我将路由直接写入login_app,则会识别它们,即使验证仍然不起作用。

你们有没有想过如何解决这个问题?是否需要更多信息?

1 个答案:

答案 0 :(得分:0)

这可能是问题:在您的login_app模块中导出router,但是您实际上没有在router上定义任何路由,它们都是在新的实例上定义的app

在一个文件中创建路由器并将其导出以在主app.js文件中使用的简单示例如下所示:

/** app.js */
var express = require('express');
var app = express();
var myRoutes = require('./routes');
app.use(myRoutes);
app.listen(3000);


/** routes.js */
// instaniate a new router, add routes/middleware on it, and export it
var router = require('express').Router();
// define your routes and middleware here
// router.use( ... )
module.exports = router;

我认为scotch.io教程正在使用的另一种模式是让路径文件导出一个函数,您将主应用实例传递到该函数中。然后这两个文件看起来像这样:

/** app.js */
var express = require('express');
var app = express();
require('./routes')(app); // pass your main `app` instance into the function
app.listen(3000);


/** routes.js */
module.export = function(app) {
  // define all your routes and middleware here directly on the `app` that gets passed in
  // app.use( ... )
}