尝试在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);
答案 0 :(得分: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()
?