I am new to passport JS , and i was making my first program in passport, i had app.js in my main directory and user.js in the models directory inside the main directory. When i tried to run the command node app.js i recieved the following error.
C:\Users\RAJ\Desktop\webD\auth\app.js:26
passport.serializeUser(User.serializeUser()); //encrypt
^
TypeError: User.serializeUser is not a function
at Object.<anonymous> (C:\Users\RAJ\Desktop\webD\auth\app.js:26:29)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
at startup (bootstrap_node.js:193:16)
at bootstrap_node.js:617:3
Following is my two files app.js and user.js.
app.js
var express = require('express'),
mongoose = require('mongoose'),
bodyParser = require('body-parser'),
passport = require('passport'),
User = require('./models/user'),
localStrategy = require('passport-local'),
passportLocalMongoose = require('passport-local-mongoose');
mongoose.connect("mongodb://localhost/auth_demo");
var app = express();
app.set('view engine','ejs');
app.get("/",function(req,res){
res.render("home");
});
app.use(require("express-session")({
secret : "some random shit",
resave: false,
saveUninitialized: false
}));
//setting up passport
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(User.serializeUser()); //encrypt
passport.deserializeUser(User.deserializeUser()); //decrypt
app.get("/secret",function(req,res)
{
res.render("secret");
});
app.get("/register",function(req,res)
{
res.render("register");
});
app.listen(8000,function(){
console.log("server has started running");
});
user.js
var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");
var UserSchema = new mongoose.Schema({
username: String,
password: String
});
UserSchema.plugin(passportLocalMongoose);
module.export = mongoose.model("User",UserSchema);
Below is the list dependencies in package.json that I installed.
"dependencies": {
"body-parser": "^1.18.2",
"ejs": "^2.5.7",
"express": "^4.16.2",
"express-session": "^1.15.6",
"mongoose": "^5.0.8",
"passport": "^0.4.0",
"passport-local": "^1.0.0",
"passport-local-mongoose": "^5.0.0"
}
The export module is wrong is:
Is not module.export
module.export = mongoose.model("User",UserSchema);
but is module.exports
module.exports = mongoose.model("User", UserSchema);
You need to build your own serializeuser() and deserializeUser() functions.
serializeUser is used to store id of the user in the session,while deserializeUser is used to retrieve the user details of the user by fetching the id from the session and then fetching the whole user details from your database. Example:
passport.serializeUser(function(req, user, done) {
done(null, user.user_id);
});
passport.deserializeUser(function(user_id, done) {
getUserInfo(user_id).then(function(user) {
return done(null, user);
}, function(err) {
return done(err,null);
});
});
this app.js worked with me try it, I think it is the same syntax as yours
var express = require("express");
var mongoose = require("mongoose");
var passport = require("passport");
var bodyParser = require("body-parser");
var localStrategy = require("passport-local");
var passportLocalMongoose = require("passport-local-mongoose");
var User = require("./models/user.js");
var app = express();
app.use(require("express-session")({
secret: "this is a secret message",
resave: false,
saveUninitialized: false,
}))
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use(passport.initialize())
app.use(passport.session())
mongoose.connect("mongodb://localhost/auth", {useNewUrlParser: true, useUnifiedTopology: true});
// passport.serializeUser(User.serializeUser())
// passport.deserializeUser(User.desrializeUser())
passport.serializeUser(User.serializeUser()); //encrypt
passport.deserializeUser(User.deserializeUser()); //decrypt
app.get("/", function(req, res){
res.render("home")
});
app.get("/secret", function(req, res){
res.render("secret")
})
app.listen(3000, function(){
console.log("server started!")
})
app.use(expressSession({ secret:"akdfjkjfkla kajfk kajfk kajfksajfk", resave:false, saveUninitialized:false, })); try this;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.