需要通行证之后,“ TypeError:无法读取未定义的属性'isAuthenticated'”

时间:2019-07-01 08:49:54

标签: node.js authentication passport.js

我想在用户访问路由user/profile之前检查用户是否已通过身份验证。为此,我使用了护照上的isAuthenticated()

但是当我运行npm start时,终端机回复了TypeError: Cannot read property 'isAuthenticated' of undefined。这是什么问题?

我的用户路线users.js

var express = require('express');
var router = express.Router();
var csrf = require("csurf");
var passport = require("passport");

const {check, validationResult} = require('express-validator');

var csrfProtection = csrf();
router.use(csrfProtection);

// User Signup
router.get("/signup", (req, res, next) => {
    var messages = req.flash("error");
    res.render("user/signup", {csrfToken: req.csrfToken(), messages: messages, hasErrors: messages.length > 0});
  });

  router.post("/signup",
  [
    check('email', 'Your email is not valid').not().isEmpty().isEmail().normalizeEmail(),
    check('password', 'Your password must be at least 5 characters').not().isEmpty().isLength({min: 5})
  ],
  function (req, res) {
    const errors = validationResult(req);
    console.log(req.body);

    if (!errors.isEmpty()) {
      return res.status(422).jsonp(errors.array())
    } else {
      passport.authenticate("local.signup", {
        successRedirect: "/user/profile",
        failureRedirect: "/user/signup",
        failureFlash: true
      })(req, res);
    }
  });

  // User signin
  router.get("/signin", (req, res, next) => {
    var messages = req.flash("error");
    res.render("user/signin", {csrfToken: req.csrfToken(), messages: messages, hasErrors: messages.length > 0});
  });

  router.post("/signin",
  [
    check('email', 'Your email is not valid').not().isEmpty().isEmail().normalizeEmail(),
    check('password', 'Your password must be at least 5 characters').not().isEmpty()
  ],
  function (req, res) {
    const errors = validationResult(req);
    console.log(req.body);

    if (!errors.isEmpty()) {
      return res.status(422).jsonp(errors.array())
    } else {
      passport.authenticate("local.signin", {
        successRedirect: "/user/profile",
        failureRedirect: "/user/signin",
        failureFlash: true
      })(req, res);
    }
  });

function isLoggedIn(req, res, next) {
    if (req.isAuthenticated()) {
    return next();
  }
    res.redirect("/");
}

// User profile
router.get("/profile", isLoggedIn(), (req, res, next) => {
    res.render("user/profile");
});

router.get("/logout", function(req, res, next) {
    req.logout();
    res.redirect("/");
});

  module.exports = router;

错误消息:

/mnt/e/CODING/Templates/leaftunehoney/SC2/routes/users.js:64
    if (req.isAuthenticated()) {
            ^

TypeError: Cannot read property 'isAuthenticated' of undefined
    at isLoggedIn (/mnt/e/CODING/Templates/leaftunehoney/SC2/routes/users.js:64:13)
    at Object.<anonymous> (/mnt/e/CODING/Templates/leaftunehoney/SC2/routes/users.js:71:24)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/mnt/e/CODING/Templates/leaftunehoney/SC2/app.js:15:18)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)

1 个答案:

答案 0 :(得分:0)

isLoggedIn是一个函数。 函数的要求来自哪里?

这是一个函数调用,这就是为什么在函数内部未定义 req 的原因,因为没有参数传递给该函数,因此没有传递给您的行: 如果(req.isAuthenticated())

错误即将来临:无法读取未定义的属性'isAuthenticated'。