ошибка показывает, что
events.js: 180 throw err;// Необработанное событие «ошибка» ^
Ошибка [ERR_UNHANDLED_ERROR]: необработанная ошибка.(Недопустимый хэш BCrypt.) В Function.emit (events.js: 178: 17)
извините, если слишком глупый вопрос, я только начал изучать узел .. Пожалуйста, помогите ..
Passport.js в папке паспорта
var passport = require('passport');
var User = require('../models/user');
var LocalStrategy = require('passport-local').Strategy;
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use('local.signup', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
}, function(req, email, password, done) {
User.findOne({
'email': email
}, function(err, user) {
if (err) {
return done(err);
}
if (user) {
return done(null, false, {
message: 'Email is already in use.'
});
}
var newUser = new User();
newUser.email = email;
newUser.password = newUser.encryptPassword(password);
newUser.save(function(err, result) {
if (err) {
return done(err);
}
return done(null, newUser);
});
});
}));
passport.use('local.signin', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
}, function(req, email, password, done) {
User.findOne({
'email': email
}, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false, {
message: 'No user found.'
});
}
if (!user.validPassword(password)){
return done(null, false, {
message: 'Wrong password.'
});
}
return done(null, user);
});
}));
это пользовательская схема, user.js внутри модуля
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt-nodejs');
var userSchema = new Schema({
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
});
userSchema.method('encryptPassword', function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(5), null);
});
userSchema.method('validPassword', function(password) {
return bcrypt.compareSync(password, this.password);
});
module.exports = mongoose.model('User', userSchema);
это маршрутизатор внутри папки маршрутов, index.js
var express = require('express');
var router = express.Router();
var csrf = require('csurf');
var passport = require('passport');
var Boi = require('../models/boi');
var csrfProtection = csrf();
router.use(csrfProtection);
/* GET home page. */
router.get('/', function(req, res, next) {
var boi = Boi.find(function(err, docs) {
var boiChunks = [];
var chunkSize = 3;
for (var i = 0; i < docs.length; i += chunkSize) {
boiChunks.push(docs.slice(i, i + chunkSize));
}
res.render('library/index', {
title: 'Boipoka',
boi: docs
});
});
});
router.get('/user/signup', function(req, res, next) {
var messages = req.flash('error');
res.render('user/signup', {
csrfToken: req.csrfToken(),
messages: messages,
hasErrors: messages.length > 0
});
});
router.post('/user/signup', passport.authenticate('local.signup', {
successRedirect: '/user/profile',
failureRedirect: '/user/signup',
failureFlash: true
}));
router.get('/user/profile', function(req, res, next) {
res.render('user/profile');
});
router.get('/user/signin', function(req, res, next) {
var messages = req.flash('error');
res.render('user/signin', {
csrfToken: req.csrfToken(),
messages: messages,
hasErrors: messages.length > 0
});
});
router.post('/user/signin', passport.authenticate('local.signin', {
successRedirect: '/user/profile',
failureRedirect: '/user/signin',
failureFlash: true
}));
router.get('/user/myshelf', function(req, res, next) {
res.render('user/myshelf');
});
module.exports = router;
вот основное приложение, app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var expressHbs = require('express-handlebars');
var mongoose = require('mongoose');
var session = require('express-session');
var passport = require('passport');
var flash = require('connect-flash');
var routes = require('./routes/index');
var app = express();
mongoose.connect('mongodb://localhost:27017/boipoka', {
useNewUrlParser: true
});
require('./config/passport');
// view engine setup
app.engine('.hbs', expressHbs({
defaultLayout: 'layout',
extname: '.hbs'
}));
app.set('view engine', '.hbs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(session({
secret: 'mysupersecret',
resave: false,
saveUninitailized: false
}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
Заранее спасибо ^ _ ^