1
我有一個簡單的auth面板與書架和護照,但是當我嘗試登錄時,我得到白色空白頁500.我嘗試所有解決方案,我在網上找到。請幫忙。bookshelf.js does not work with passport
配置/ passport.js
...
passport.use('local-login', new LocalStrategy({
passReqToCallback : true
},
function(req, username, password, done) {
User.where({username: username}).fetch().then(function(err, user) {
if (err) return done(err);
if (!user) {
return done(null, false, req.flash('loginMessage', 'Cant find user!'));
}
user = user.toJSON();
if(User.validPassword(password, user.password)){
return done(null, false, req.flash('loginMessage', 'Invalid pass!'));
}
return done(null, user);
}).catch(function(err) {
console.error(err);
});
}
));
...
路線/ auth.js
...
router.post('/login', passport.authenticate('local-login', {
successRedirect: '/users/profile',
failureRedirect: '/auth/login',
failureFlash: true
}));
...
server.js
const express = require('express');
const session = require('express-session');
const passport = require('passport');
const flash = require('connect-flash');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const config = require('./config/app.js');
const index = require('./routes/index');
const auth = require('./routes/auth');
const users = require('./routes/users');
require('./config/passport.js')(passport);
const app = express();
app.set('env', config.website.env);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')))
app.use(session({
secret: config.security.salt,
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use('/', index);
app.use('/auth', auth);
...
如果密碼相同,則'validPassword()'方法返回false。感謝它的作品。 – mcek