gpt4 book ai didi

node.js - Passport + Node - req.isAuthentiated 不是 Object.isLoggedIn 中的函数

转载 作者:太空宇宙 更新时间:2023-11-04 00:41:38 26 4
gpt4 key购买 nike

我的 Passport 功能代码有错误。

我遵循了本教程:Sample tutorial followed

我从 nodemon server.js 中的/routes 文件收到错误:

TypeError: req.isAuthentiated is not a function

当我尝试登录时,浏览器窗口中出现此错误(我可以注册,并且数据库正在存储用户):

TypeError: req.isAuthentiated is not a function at Object.isLoggedIn [as handle] 

这是 Passport 功能,我对如何解决这个问题感到困惑。

服务器.js:

var express  = require('express');
var app = express();
var port = process.env.PORT || 8080;
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash'); // messages stored in session

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

var configDB = require('./config/database.js');

// configuration
mongoose.connect(configDB.url); // connect to our database

require('./config/passport')(passport); // pass passport for configuration

// set up our express application
app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser.json()); // get information from html forms
app.use(bodyParser.urlencoded({ extended : true})); // for express 4
app.set('view engine', 'ejs'); // set up ejs for templating

// required for passport
app.use(session({ secret: 'ilovescotchscotchyscotchscotch' })); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session

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

// launch
app.listen(port);
console.log('The magic happens on port ' + port);

routes.js

    var flash = require('connect-flash');
var express = require('express');
var router = express.Router();

//module.exports = function (app, passport){
module.exports = function (app, passport){
// get homepage
app.get('/', function(req, res){
res.render('index.ejs');
});
// show login form
app.get('/login', function(req, res){
// render page and pass in any flash data i(f there is any)
res.render('login.ejs', {message: req.flash('loginMessage') });
});

// process login form
app.get('/signup', function(req, res) {
//render page, pass in any flash data (if there is any)
res.render('signup.ejs', {message: req.flash('signupMessage') });
});

// process signup form
// app.post('/signup', do all passport stuff here);

// PROFILE SECTION
// route to middleware to verify this (w/ isLoggedIn fn)
app.get('/profile', isLoggedIn, function(req, res) {
res.render('profile.ejs', {
user: req.user //get user out of session pass to template
});
});

// LOG OUT
app.get('/logout', function(req, res) {
req.logout(); // provided by passport
res.redirect('/');
});

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

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

return router;
};


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

// if user is logged in -
if (req.isAuthentiated())
return next();

// if they aren't redirect them to home
res.redirect('/');
}

最佳答案

function isLoggedIn(req, res, next) {
if (req.isAuthenticated()) // <-- typo here
return next();
res.redirect('/');
}

关于node.js - Passport + Node - req.isAuthentiated 不是 Object.isLoggedIn 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36478711/

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