本地护照和本地护照猫鼬的身份验证错误

Posted

技术标签:

【中文标题】本地护照和本地护照猫鼬的身份验证错误【英文标题】:authentication error with passport-local and passport-local-mongoose 【发布时间】:2016-10-19 02:16:51 【问题描述】:

尝试在我的网络应用程序中使用 node.js 上的 passport、passport-local 和 pasport-local-mongoose(使用 cloud9)实现身份验证。

我的身份验证成功,但是当我尝试重定向到我的应用程序中的其他页面时(重定向到 http://www.google.com 有效!)我得到了这个堆栈跟踪(需要一个方向):

TypeError: user.get is not a function
    at /home/ubuntu/workspace/attendance/v4/node_modules/passport-local-mongoose/index.js:217:21
    at pass (/home/ubuntu/workspace/attendance/v4/node_modules/passport/lib/authenticator.js:347:9)
    at Authenticator.deserializeUser (/home/ubuntu/workspace/attendance/v4/node_modules/passport/lib/authenticator.js:352:5)
    at SessionStrategy.authenticate (/home/ubuntu/workspace/attendance/v4/node_modules/passport/lib/strategies/session.js:53:28)
    at attempt (/home/ubuntu/workspace/attendance/v4/node_modules/passport/lib/middleware/authenticate.js:348:16)
    at authenticate (/home/ubuntu/workspace/attendance/v4/node_modules/passport/lib/middleware/authenticate.js:349:7)
    at Layer.handle [as handle_request] (/home/ubuntu/workspace/attendance/v4/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/ubuntu/workspace/attendance/v4/node_modules/express/lib/router/index.js:312:13)
    at /home/ubuntu/workspace/attendance/v4/node_modules/express/lib/router/index.js:280:7
    at Function.process_params (/home/ubuntu/workspace/attendance/v4/node_modules/express/lib/router/index.js:330:12)
    at next (/home/ubuntu/workspace/attendance/v4/node_modules/express/lib/router/index.js:271:10)
    at initialize (/home/ubuntu/workspace/attendance/v4/node_modules/passport/lib/middleware/initialize.js:53:5)
    at Layer.handle [as handle_request] (/home/ubuntu/workspace/attendance/v4/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/ubuntu/workspace/attendance/v4/node_modules/express/lib/router/index.js:312:13)
    at /home/ubuntu/workspace/attendance/v4/node_modules/express/lib/router/index.js:280:7
    at Function.process_params (/home/ubuntu/workspace/attendance/v4/node_modules/express/lib/router/index.js:330:12)

我的 app.js 如下:

var express = require('express'),
    app = express(),
    BodyParser = require("body-parser"),
    mongoose = require("mongoose"),
    students_class = require("./models/class"),
    passport= require("passport"),
    localstrategy=require ("passport-local"),
 //   passportlocalmongoose=("passport-local-mongoose"),
    student = require ("./models/student");
    mongoose.connect("mongodb://localhost/attendance");



app.set("view engine", "ejs");
app.use(express.static ("public"));
app.use(BodyParser.urlencoded(extended: true));
app.use (require("express-session")( 
    secret: "liran",
    resave:false,
    saveUninitialized:false
));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new localstrategy(student.authenticate()));    
passport.serializeUser(student.serializeUser());
passport.deserializeUser(student.serializeUser());

// ====
// routes
// =====
app.get("/", function(req, res) 
    student.find(, function(err, student) 
        if (err) 
            console.log(err);
         else 
            res.render("home/index.ejs", 
                students: student
            );
        

    );
);

app.post ("/login",passport.authenticate("local"),function (req,res) 
    res.redirect("/");

)

和我的用户模型:

var mongoose = require("mongoose"),
    passportlocalmongoose=require("passport-local-mongoose")
var studentsSchema = new mongoose.Schema(
    username: String,
    passport:String
   // name: String,
 //   image: String,
 //   description:String,
   // classes: [ type: mongoose.Schema.Types.ObjectId, ref: 'class' ]
);


studentsSchema.plugin(passportlocalmongoose);
module.exports= mongoose.model("student", studentsSchema);

【问题讨论】:

【参考方案1】:

让我们检查堆栈跟踪,看看是否能找到提示:

TypeError: user.get is not a function
    at /home/ubuntu/workspace/attendance/v4/node_modules/passport-local-mongoose/index.js:217:21
    at pass (/home/ubuntu/workspace/attendance/v4/node_modules/passport/lib/authenticator.js:347:9)
    at Authenticator.deserializeUser (/home/ubuntu/workspace/attendance/v4/node_modules/passport/lib/authenticator.js:352:5)
    ...

嗯,deserializeUser 可能是导致问题的原因。

让我们看看它是如何设置的:

passport.serializeUser(student.serializeUser());
passport.deserializeUser(student.serializeUser());

也许您打算将student.deserializeUser() 传递给passport.deserializerUser()

【讨论】:

以上是关于本地护照和本地护照猫鼬的身份验证错误的主要内容,如果未能解决你的问题,请参考以下文章

如何修复阻止注册用户登录的护照本地身份验证错误

节点护照错误:未知身份验证策略“本地登录”

mongoose 中本地护照的身份验证错误

护照 未知的身份验证策略“本地”、“脸书”、“谷歌”

护照本地猫鼬中的密码验证器选项不起作用

在服务器端护照js本地用户身份验证后,如何在客户端使用cookie显示用户信息?