2017-04-26 25 views
0

passport.jsとmongoose認証を設定した後、関連するすべてのステートメントで「解決されていない関数またはメソッド」が一貫して取得されています。Passport.JSで未解決の関数またはメソッド

enter image description here enter image description here

私はしかし、問題が続いている、chachesを無効にし、他の回避策と一緒に再起動してみました。

マイuser.jsの:

var Schema = mongoose.Schema; 
var passportLocalMongoose = require('passport-local-mongoose'); 

//create user schema and model 
var User = new Schema({ 
    username: String, 
    password: String, 
    studentID: String, 
    grades: [{ 
     subject: String, 
     grade: String 
    }] 
}); 

User.plugin(passportLocalMongoose); 

module.exports = mongoose.model('user', User); 

When I run the server, I also get the error:

TypeError: passport.serialize is not a function

+1

かなり役に立たない写真、しかし - 私は 'モデル/ user.js'それを取るあなたが書いたものです - あなたは、おそらくそれは間違って書いた - それはあなたの(ではないそれの写真)だ場合、コードを投稿し、言うことができる –

+0

@ JaromandaXの写真は、Webstormがメソッドを認識していないことを示すためにのみ含まれています。私は謝罪します。 – Markoe7

+0

私は理解しますが、コードを助けたい場合は、おそらく間違っている**コードを投稿してください。 jsはあなたのコードです) –

答えて

0

user.jsの

const mongoose = require('mongoose'); 
const bcrypt = require('bcrypt-nodejs'); 

const Schema = mongoose.Schema; 

//= =============================== 
// User Schema 
//= =============================== 
const UserSchema = new Schema({ 
    email: { 
    type: String, 
    lowercase: true, 
    unique: true, 
    required: true 
    }, 
    password: { 
    type: String, 
    required: true 
    } 
    }); 


// Pre-save of user to database, hash password if password is modified or new 
UserSchema.pre('save', function (next) { 
    const user = this, 
    SALT_FACTOR = 5; 

    if (!user.isModified('password')) return next(); 

    bcrypt.genSalt(SALT_FACTOR, (err, salt) => { 
    if (err) return next(err); 

    bcrypt.hash(user.password, salt, null, (err, hash) => { 
     if (err) return next(err); 
     user.password = hash; 
     next(); 
    }); 
    }); 
}); 

// Method to compare password for login 
UserSchema.methods.comparePassword = function (candidatePassword, cb) { 
    bcrypt.compare(candidatePassword, this.password, (err, isMatch) => { 
    if (err) { return cb(err); } 

    cb(null, isMatch); 
    }); 
}; 

module.exports = mongoose.model('User', UserSchema); 

passport.js

const passport = require('passport'); 
const User = require('../models/user'); 
const config = require('./main'); 
const LocalStrategy = require('passport-local'); 

// username field is now email 
const localOptions = { 
    usernameField: 'email' 
}; 

// set up the local login strategy 
const localLogin = new LocalStrategy(localOptions, (email, password, done) => { 
    User.findOne({ email }, (err, user) => { 
    if (err) { return done(err); } 
    if (!user) { return done(null, false, { error: 'Your login details could not be verified. Please try again.' }); } 

    user.comparePassword(password, (err, isMatch) => { 
     if (err) { return done(err); } 
     if (!isMatch) { return done(null, false, { error: 'Your login details could not be verified. Please try again.' }); } 

     return done(null, user); 
    }); 
    }); 
}); 

passport.use(localLogin); 
0

これで、ユーザー登録などのビジネスロジックを処理するコントローラを作成するだけで済みます。例えば、

* UserController.js *

// other functions 

// register 
export function register() { 
    if (!req.body) { 
    res.status(500).json({ error: 'All Fields Required.' }); 
    } 

    const user = new User(req.body) 

    user.save((err, user) => { 
    if (err) { 
     res.status(500).json({ err }); 
    } 
    res.status(200).json({ user }); 
    }) 
} 

router.js

// other important things to require 
const passportService = require('./config/passport'); 

// Middleware to require login/auth 
const requireLogin = passport.authenticate('local', { session: false 
}); 

// your routes go here and you can access requireLogin now, like so: 

router.get('/users/:id', requireLogin, getUserById); 
router.post('/users', register); 

・ホープ、このことができます。あなたのコードベースを変更するつもりはありませんが、これはあなたがしたいことをする良い方法です。かなり

+0

私は応答を感謝しますが、私の問題はセットアップではなく、 "未解決の関数またはメソッド"エラーです – Markoe7

+0

リンクを追加することはおそらく助けになります。あなたが与えたものは、ほとんどなくなります。 –

+0

https://github.com/Scyon/mtsd-schoolbook – Markoe7

関連する問題