Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- passport.use(new LocalStrategy({
- usernameField: 'email',
- passwordField: 'password'
- },
- function(username, password, cb) {
- UserSchema.findByUsername(username, function(err, user) {
- if (err) { return cb(err); }
- if (!user) { return cb(null, false); }
- if (user.password != password) { return cb(null, false); }
- return cb(null, user);
- });
- }));
- router.post('/login',
- passport.authenticate('local', {
- failureRedirect: '/login' }),
- function(req, res) {
- res.redirect('/');
- });
- const UserSchema = mongoose.Schema ({
- firstName: {
- type: String,
- index: true
- },
- lastName: {
- type: String,
- index: true
- },
- email: {
- type: String,
- index: true
- },
- password: {
- type: String,
- index: true
- },
- homeAirport: {
- type: String,
- index: true
- }
- })
- UserSchema.plugin(passportLocalMongoose)
- // Adding this function to find by username
- exports.findByUsername = function(username, cb) {
- process.nextTick(function() {
- for (var i = 0, len = records.length; i < len; i++) {
- var record = records[i];
- if (record.username === username) {
- return cb(null, record);
- }
- }
- return cb(null, null);
- });
- }
- // Export it to the app
- module.exports = mongoose.model('user', UserSchema)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement