gpt4 book ai didi

node.js - 路线 护照-本地

转载 作者:太空宇宙 更新时间:2023-11-04 01:51:22 27 4
gpt4 key购买 nike

我正在尝试使用 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;

为了简化问题,我只将 paths.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,即使身份验证仍然不起作用,它们也会被识别。

你们中有人知道如何解决这个问题吗?需要更多信息吗?

最佳答案

这可能是问题所在:在您的 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( ... )
}

关于node.js - 路线 护照-本地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49538041/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com