Я новичок в nodejs и пытаюсь создать систему входа в систему. Я добавил код для сериализации пользователя из паспортной документации в config / passport.js . Но все же я получаю ошибку: Не удалось сериализовать пользователя в сеанс при вводе действительного адреса электронной почты и пароля для входа в систему. Кто-нибудь может указать, что я делаю неправильно?
app.js
const express = require('express');
const expressLayouts = require('express-ejs-layouts');
const mongoose = require('mongoose');
const flash = require('connect-flash');
const session = require('express-session');
const passport = require('passport');
const app = express();
//Passport config
require('./config/passport')(passport);
//DB config
require('dotenv').config(); //for setting environment variables on server
const uri = process.env.ATLAS_URI;
mongoose.connect(uri,{useNewUrlParser:true ,useUnifiedTopology: true})
.then(() => console.log("mongodb connected"))
.catch(err => console.log(err));
//EJS
app.use(expressLayouts);
app.set('view engine','ejs');
//Express Bodyparser
app.use(express.urlencoded({extended:true}));
//Express session
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true,
}));
//Passport middleware for authentication and login
app.use(passport.initialize());
app.use(passport.session());
//connect flash
app.use(flash());
//global variables to create flash messages
app.use((req,res,next) => {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
next();
});
//Routes
app.use('/',require('./routes/index'));
app.use('/users',require('./routes/users'));
const PORT = process.env.PORT || 5000;
app.listen(PORT,console.log(`Server started on port ${PORT}`));
config / passport.js
const localStrategy = require('passport-local').Strategy;
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
//load user model
const User = require('../models/User');
module.exports = function(passport) {
passport.use(
new localStrategy({usernameField:'email'},(email,password,done) => {
//Check if email is registered
User.findOne({email:email})
.then( user => {
if(!user){
return done(null,false,{message:'This email is not registered'});
}
//if email exists we compare password
bcrypt.compare(password,user.password,(err,isMatch) => {
if(err) throw err;
if(isMatch){
return done(null,true);
}
else{
return done(null,false,{message:'Wrong Password!'});
}
});
}
)
.catch(err => console.log(err));
})
);
//Serialize and deserialize user
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
};
routs / users.js
const express = require('express');
const router = express.Router();
const User = require('../models/User');
const bcrypt = require('bcryptjs');
const passport = require('passport');
//Login page
router.get('/login',(req,res)=>res.render('login'));
//Register
router.get('/register',(req,res)=>res.render('register'));
//Registration Handling
router.post('/register',(req,res) =>{
const { name, email, password, password2 } = req.body;
let errors = [];
//Check required fields
if(!name || !email ||!password || !password2){
errors.push({msg:'Please fill required fields'});
}
//check if passwords equal
if(password !== password2){
errors.push({msg:'Passwords do not match'});
}
//check password length
if(password.length<6){
errors.push({msg:'Password should be atleast 6 characters'});
}
if(errors.length>0){
res.render('register',{
errors,
name,
email,
password,
password2
});
}else{
//Validation passed
//check if email already exists
User.findOne({email:email})
.then(user => {
if(user){
errors.push({msg:'Email already registered'});
res.render('register',{
errors,
name,
email,
password,
password2,
});
}
else{
const newUser = new User({
name,
email,
password,
});
//Hash password
bcrypt.genSalt(10,(err,salt) => {
if(err) throw err;
bcrypt.hash(newUser.password,salt,(err,hash) => {
if(err) throw err;
//set password to hash
newUser.password = hash
//save user
newUser.save()
.then( user => {
req.flash('success_msg','Thanks for registering!You can login now');
res.redirect('/users/login');
})
.catch(err => {
console.log(err);
});
});
});
}
});
}
});
//Login handling
router.post('/login',(req,res,next) => {
passport.authenticate('local',{
successRedirect:'/dashboard',
failureRedirect:'/users/login', //these routes are wrt host
failureFlash: true,
})(req,res,next);
});
module.exports = router