2016-10-13 6 views
0

こんにちは、私は新しいことを表現して、ユーザーとしてログインしようとしています。次のコードでは、私は以下のエラーが表示されます。私はmodule.exports.comparePasswordを実行して以来、私は考えました。私は行方不明のものがありますか?おかげで任意のヘルプは機能に問題がありませんPassportでログインする

events.js:160 throw er; // Unhandled 'error' event ^

TypeError: user.comparePassword is not a function at /Users/Sam/Desktop/teach/routes/users.js:112:17 at Query. (/Users/Sam/Desktop/teach/node_modules/mongoose/lib/model.js:3343:16) at /Users/Sam/Desktop/teach/node_modules/kareem/index.js:259:21 at /Users/Sam/Desktop/teach/node_modules/kareem/index.js:127:16 at _combinedTickCallback (internal/process/next_tick.js:67:7) at process._tickCallback (internal/process/next_tick.js:98:9)

モデル/ user.jsの

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 
var bcrypt = require('bcryptjs') 

var userSchema = new Schema({ 
    email: { type: String }, 
    password: { type: String }, 
    type: { type: String } 
}); 

var User = mongoose.model('User', userSchema); 

module.exports = User; 

//Get a User by id 
module.exports.getUserById = function(id, callback){ 
    User.findById(id, callback); 
} 

//Get a User by email 
module.exports.getUserByEmail = function(email, callback){ 
    var query = {email : email} 
    User.findOne(query, callback); 
} 

//Save a student 
module.exports.saveStudent = function(newUser, newStudent, callback){ 
    bcrypt.genSalt(10, function(err, salt) { 
     bcrypt.hash(newUser.password, salt, function(err, hash){ 
      if (err) {throw err} 
      newUser.password = hash; 
      //Saves both a user and student 
      async.parallel([newUser.save, newStudent.save], callback); 
     }) 
    }) 
} 

//Save a instructor 
module.exports.saveInstructor = function(newUser, newInstructor, callback){ 
    bcrypt.genSalt(10, function(err, salt) { 
     bcrypt.hash(newUser.password, salt, function(err, hash){ 
      if (err) {throw err} 
      newUser.password = hash; 
      //Saves both a user and instructor 
      async.parallel([newUser.save, newInstructor.save], callback); 
     }) 
    }) 
} 

//Checks if password matches. 
module.exports.comparePassword = function(candidatePassword, hash, callback){ 
    bcrypt.compare(candidatePassword, hash, function(err, isMatch){ 
     if(err) throw err; 
     callback(null, isMatch); 
    }); 
} 

ルート/ users.js

var express = require('express'); 
var router = express.Router(); 
var passport = require('passport'); 
var LocalStrategy = require('passport-local').Strategy; 

var User = require('../models/user'); 
var Student = require('../models/student'); 
var Instructor= require('../models/instructor'); 

router.get('/signup', function(req, res, next) { 
    res.render('users/signup'); 
}); 

router.get('/login', function(req, res, next){ 
    res.render('users/login') 
}); 

//Registering a user 
router.post('/signup', function(req, res, next){ 

    var first_name  = req.body.first_name; 
    var last_name  = req.body.last_name; 
    var email   = req.body.email; 
    var password  = req.body.password; 
    var password2  = req.body.password2; 
    var type   = req.body.type; 

    req.checkBody('first_name', 'First name is required.').notEmpty(); 
    req.checkBody('first_name', 'Please enter a shorter first name.').len(1, 40); 
    req.checkBody('last_name', 'Last name is required.').notEmpty(); 
    req.checkBody('last_name', 'Please enter a shorter last name.').len(1, 40); 
    req.checkBody('email', 'Email is required.').notEmpty(); 
    req.checkBody('email', 'Email must be valid.').isEmail(); 
    req.checkBody('email', 'Please enter a shorter email.').len(1, 40); 
    req.checkBody('password', 'Password is required.').notEmpty(); 
    req.checkBody('password2', 'Passwords must match.').equals(req.body.password); 
    req.checkBody('password', 'Please choose a password between 6 to 50 characters.').len(6, 50); 

    var errors = req.validationErrors(); 

    if(errors){ 
     res.render('users/signup', { 
      errors: errors, 
      first_name: first_name, 
      last_name: last_name, 
      email: email, 
      password: password, 
      password2: password2 
     }); 
    } else { 
     var newUser = new User({ 
      email: email, 
      password: password, 
      type: type 
     }); 

     var newStudent = new Student({ 
      first_name: first_name, 
      last_name: last_name, 
      email: email, 
     }); 

     var newInstructor = new Instructor({ 
      first_name: first_name, 
      last_name: last_name, 
      email: email, 
     }); 

     if(type == 'student'){ 
      User.saveStudent(newUser, newStudent, function(err, user){ 
       console.log('Student saved'); 
      }); 
     } else { 
      User.saveInstructor(newUser, newInstructor, function(err, user){ 
       console.log('Instructor saved'); 
      }); 
     } 

     res.redirect('/classes'); 
    } 
}); 

passport.serializeUser(function(user, done){ 
    done(null, user._id); 
}); 

passport.deserializeUser(function(id, done){ 
    User.getUserByEmail(function(err, user){ 
     done(err, user); 
    }); 
}); 

//Login in a user 
router.post('/login',passport.authenticate('local', { 
    failureRedirect:'/users/login', 
    failureFlash:'Wrong Username or Password' 
}), function(req, res){ 
    var usertype = req.user.type; 
    res.redirect('/classes'); 
}); 

passport.use(new LocalStrategy({ 
    usernameField: 'email' 
    }, 
    function(email, password, done) { 
     User.getUserByEmail(email, function(err, user){ 
      if (err) return done(err); 
      if(!user){ 
       return done(null, false, { message: 'Unregistered email'}); 
      } 
      if (!user.comparePassword(password)) { 
       return done(null, false, { message: 'Incorrect password.' }); 
      } 
      return done(null, user); 
     }); 
    } 
)); 

module.exports = router; 

答えて

0

のために明らかに、comparePassword以来、未定義の関数エラーを与えますユーザースキーマに対して定義された関数ではありません。 User.comparePasswordとして(JSファイル内に)機能があるのでUser.comparePasswordとして使用できますが、ユーザ(ユーザスキーマのオブジェクト - mongo)にはそのような関数が定義されていません。

ユーザーをエクスポートする前にこれを行い、

userSchema.methods.comparePassword = function(candidatePassword, hash, callback){ 
    bcrypt.compare(candidatePassword, hash, function(err, isMatch){ 
     if(err) throw err; 
     callback(null, isMatch); 
    }); 
}; 

はそれがお役に立てば幸いです。

0

「ユーザー」がありますが、「ユーザー」を使用しています。大文字と小文字の区別がありますコードの下 用途:

passport.use(new LocalStrategy({ 
     usernameField: 'email' 
     }, 
     function(email, password, done) { 
      User.getUserByEmail(email, function(err, user){ 
       if (err) return done(err); 
       if(!user){ 
        return done(null, false, { message: 'Unregistered email'}); 
       } 
       if (!User.comparePassword(password)) { 
        return done(null, false, { message: 'Incorrect password.' }); 
       } 
       return done(null, user); 
      }); 
     } 
    )); 

提案: あなたがやっているように何度も何度もmodule.exportsはを書くために必要はありませんので、あなたはそれをブロック全体をエクスポートすることができますよりも、あなたがmodule.exportsはを使用している場合。

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 
var bcrypt = require('bcryptjs') 

var userSchema = new Schema({ 
    email: { type: String }, 
    password: { type: String }, 
    type: { type: String } 
}); 

var User = mongoose.model('User', userSchema); 

module.exports = model; 

//Get a User by id 
model.getUserById = function(id, callback){ 
    User.findById(id, callback); 
} 

//Get a User by email 
model.getUserByEmail = function(email, callback){ 
    var query = {email : email} 
    User.findOne(query, callback); 
} 

//Save a student 
model.saveStudent = function(newUser, newStudent, callback){ 
    bcrypt.genSalt(10, function(err, salt) { 
     bcrypt.hash(newUser.password, salt, function(err, hash){ 
      if (err) {throw err} 
      newUser.password = hash; 
      //Saves both a user and student 
      async.parallel([newUser.save, newStudent.save], callback); 
     }) 
    }) 
} 

//Save a instructor 
model.saveInstructor = function(newUser, newInstructor, callback){ 
    bcrypt.genSalt(10, function(err, salt) { 
     bcrypt.hash(newUser.password, salt, function(err, hash){ 
      if (err) {throw err} 
      newUser.password = hash; 
      //Saves both a user and instructor 
      async.parallel([newUser.save, newInstructor.save], callback); 
     }) 
    }) 
} 

//Checks if password matches. 
model.comparePassword = function(candidatePassword, hash, callback){ 
    bcrypt.compare(candidatePassword, hash, function(err, isMatch){ 
     if(err) throw err; 
     callback(null, isMatch); 
    }); 
} 

model.schema = User; 
関連する問題