Search code examples
javascriptnode.jsmongoosepassport.jspassport-local

authentication error with passport-local and passport-local-mongoose


trying to implement authentication in my web-app using passport, passport-local and pasport-local-mongoose on node.js (using cloud9).

my authentication is successful, but when i'm trying to redirect to other page within my app (redirecting to http://www.google.com works!) i'm getting this stacktrace (need a direction on this):

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)

my app.js is the following:

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("/");

})

and my user model:

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);

Solution

  • Let's check the stack trace to see if we can find a hint:

    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)
        ...
    

    Hmm, deserializeUser might be causing the problem.

    Let's look at how it's set up:

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

    Perhaps you meant to pass student.deserializeUser() to passport.deserializerUser()?