app.use(passport.initialize())
app.use(passport.session())
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
var User = require('./models/user.js')
passport.use(new LocalStrategy(User.authenticate()));
//passport.use(new LocalStrategy(UserSchema.authenticate()))
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
// Connect to Mongoose
mongoose.connect('mongodb://localhost/users')
Registration route (gets data to the db, but fails to redirect)
// Register a user to the DB
router.post('/register', function(req, res, next){
let firstName = req.body.firstName
let lastName = req.body.lastName
let username = req.body.email
//let password = req.body.password
let homeAirport = req.body.homeAirport
User.register(new User ({
firstName: firstName,
lastName: lastName,
username: username,
homeAirport: homeAirport
}),
req.body.password, function(err, user) {
if (err) {
console.log(err)
return res.render('register', {
user: user
})
}
// both of these works
passport.authenticate('local', { failureRedirect: '/' }),
function(req, res, next) {
res.redirect('/');
}
})
})
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
passportLocalMongoose = require('passport-local-mongoose');
// Define the scheme
var User = new Schema ({
firstName: {
type: String,
index: true
},
lastName: {
type: String,
index: true
},
email: {
type: String,
index: true
},
homeAirport: {
type: String,
index: true
}
})
User.plugin(passportLocalMongoose)
module.exports = mongoose.model('User', User)