2017-04-06 78 views
-1

我想實現一個passport認證策略,以我的應用程序,但不斷嘗試登錄後收到以下錯誤:護照 - 類型錯誤:passport.authenticate不是一個函數

TypeError: passport.authenticate is not a function at exports.signin

我很確定我錯過了一些東西,但無法弄清楚那可能是什麼。由於在user模型中沒有任何與認證有關的內容,所以我決定不在這裏輸出它。

我的文件夾結構如下:

|-- project 
    |-- .env 
    |-- index.html 
    |-- package.json 
    |-- server.js 
    |-- app 
    | |-- config 
    | | |-- passport.js 
    | | |-- routes.js 
    | | |-- test.js 
    | | |-- passport 
    | |  |-- local.js 
    | |-- controllers 
    | | |-- users.js 
    | |-- models 
    |  |-- user.js 

這是我的routes.js文件

/* Config/Routes */ 

'use strict'; 
const users = require('../controllers/users'); 

module.exports = function(app) { 
    app.post('/login', users.signin); 
    app.post('/users', users.create); 
}; 

這是我local.jsLocalStrategy

/* Config/Passport/Local */ 

'use strict'; 
const mongoose = require('mongoose'); 
const LocalStrategy = require('passport-local').Strategy; 
const User = mongoose.model('User'); 

module.exports = new LocalStrategy({ 
    usernameField: 'email' 
    }, 
    function(email, password, done) { 
     User.findOne({email: email }, function (err, user) { 
      if (err) 
       return done(err); 

      if (!user) 
       return done(null, false, { message: 'Unknown user' }); 

      if(!user.validPassword(password)) 
       return done(null, false, { message: 'Invalid password' }); 

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

這是我的passport.js文件,其中LocalStrategy被分配到passport

/* Config/Passport */ 

'use strict'; 
const mongoose = require('mongoose'); 
const User = mongoose.model('User'); 
const local = require('./passport/local'); 

module.exports = function(passport) { 
    passport.use(local); 
}; 

這是我的users.js控制器文件:

/* Controller/Users */ 

'use strict'; 
const mongoose = require('mongoose'); 
const User = mongoose.model('User'); 
const only = require('only'); 
const passport = require('../config/passport'); 

/* Creates a new user */ 
exports.create = function(req, res) { 
    // create a new user object of request body 
    const user = new User(only(req.body, 'firstName lastName email password')); 

    // attempting to create a new user 
    user.save(function (err, user) { 
     if(err) { 
      // output validation errors 
      return res.json(err); 
     } 
     // output created user 
     return res.json(user); 
    }); 
}; 

exports.signin = function(req, res, next) { 
    passport.authenticate('local', function (err, user, info) { 
     if (err) 
      return next(err); 

     if (!user) 
      return res.status(401).json({message: info.message}); 

     req.logIn(user, function(err) { 
      next(err) 
     }); 

     res.json(req.user); 
    })(req, res, next) 
}; 

最後,我server.js文件:

/* Server.js */ 

'use strict'; 
const mongoose = require('mongoose'); 
const bodyParser = require('body-parser'); 
const config = require('./app/config/test'); 
const express = require('express'); 
const passport = require('passport'); 

const port = process.env.PORT || 3000; 
const app = express(); 

mongoose.Promise = global.Promise; 
if(mongoose.connect(config.db)) { 
    console.log("Connected to a database"); 
} 

/* Database Models */ 
const User = require('./app/models/user'); 

app.use(bodyParser.urlencoded({ extended: true })); 
app.use(bodyParser.json()); 

app.use(passport.initialize()); 

/* Bootstrap Routes */ 
require('./app/config/passport')(passport); 
require('./app/config/routes')(app); 

app.listen(port); 
console.log('Express app started on port ' + port); 

// log the current API version 
console.log('Current API version: ' + process.env.API_VERSION); 

回答

1

也許你已經找到了解決辦法,但它可能會在未來幫助其他人。您需要config/passport.js。但它不會返回護照對象。它只是撥打使用的方法。

相關問題