您能请我解释一下为什么受保护的视图无法访问同一数据库中的其他模型
使用本地策略的passport.js代码
const LocalStrategy = require('passport-local').Strategy;
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) => {
// Match user
User.findOne({
email: email
}).then(user => {
if (!user) {
return done(null, false, { message: 'That email is not registered' });
}
// Match password
bcrypt.compare(password, user.password, (err, isMatch) => {
if (err) throw err;
if (isMatch) {
return done(null, user);
} else {
return done(null, false, { message: 'Password incorrect' });
}
});
});
})
);
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id,(err, user) => {
done(err, user);
});
});
};
User.js(模型)
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
admin: {
type: String,
default: false
},
date: {
type: Date,
default: Date.now
}
});
const User = mongoose.model('User', UserSchema);
module.exports = User;
Phone.js(模型)<-这是我要在受保护的视图中显示的新模型。
const mongoose = require('mongoose');
const PhoneSchema = new mongoose.Schema({
phone_number: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
}
});
const Phone = mongoose.model('Phone', PhoneSchema);
module.exports = Phone;
index.js
const express = require('express');
const router = express.Router();
const {
ensureAuthenticated
} = require('../config/auth');
// Welcome Page
router.get('/', (req, res) => res.render('welcome'));
// Dashboard
router.get('/dashboard', ensureAuthenticated, (req, res) => {
res.render('dashboard', {
user: req.user
})
});
// Input
router.get('/input', ensureAuthenticated, (req, res) => {
res.render('input', {
})
});
// Phone view
router.get('/phones', ensureAuthenticated, (req, res) => {
res.render('phones', {
user: req.user
})
});
// Phone view
router.get('/developer', ensureAuthenticated, (req, res) => {
res.render('developer', {
})
});
module.exports = router;
user.js
const express = require('express');
const router = express.Router();
const bcrypt = require('bcryptjs');
const passport = require('passport');
// Load User model
const User = require('../models/User');
// Login Page
router.get('/login', (req, res) => res.render('login'));
// Register Page
router.get('/register', (req, res) => res.render('register'));
// Register
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 enter all fields'
});
}
if (password != password2) {
errors.push({
msg: 'Passwords do not match'
});
}
if (password.length < 6) {
errors.push({
msg: 'Password must be at least 6 characters'
});
}
if (errors.length > 0) {
res.render('register', {
errors,
name,
email,
password,
password2
});
} else {
User.findOne({
email: email
})
.then(user => {
if (user) {
errors.push({
msg: 'Email already exists'
});
res.render('register', {
errors,
name,
email,
password,
password2
});
} else {
const newUser = new User({
name,
email,
password
});
// Hash password
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
if (err) throw err;
// Set password to hashed
newUser.password = hash;
// Save user
newUser.save()
.then(user => {
req.flash('success_msg', 'You are now registered and can log in');
res.redirect('/users/login');
})
.catch(err => console.log(err));
});
});
}
});
}
});
// Login
router.post('/login', (req, res, next) => {
passport.authenticate('local', {
successRedirect: '/dashboard',
failureRedirect: '/users/login',
failureFlash: true
})(req, res, next);
});
// Logout
router.get('/logout', (req, res) => {
req.logout();
req.flash('success_msg', 'You are logged out');
res.redirect('/users/login');
});
module.exports = router;
当我尝试将电话模型渲染到/ phone视图时,它给我错误
这个项目属于traversymedia,我只想知道如何使用passportJS向受保护的视图显示其他模型。