简体   繁体   中英

Passport.js .authenticate() throwing TypeError: undefined is not a function

I'm new to passport (and node in general) and have been using this tutorial ( http://mherman.org/blog/2015/01/31/local-authentication-with-passport-and-express-4/#.VczPbTBViko ) to add passport authentication to a larger web app. I think I have been following things correctly, but when I start the app in terminal, I get the following error.

passport.use(new LocalStrategy(Admin.authenticate()));
                                     ^
TypeError: undefined is not a function
    at Object.<anonymous> (/Users/goldru/design-data/app.js:37:38)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/Users/goldru/design-data/bin/www:7:11)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

I checked package.json and all of the relevant libraries appear to be installed.

Here is the relevant code.

from app.js:

//connect to db; require models
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/design-data-test');
var Test = require('./models/Tests');
var Question = require('./models/Question');
var User = require('./models/User');
var Option = require('./models/Option');
var Admin = require('./models/Admin');

//require passport
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;

var routes = require('./routes/index');

var express = require('express');
var app = express();

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

//passport setup
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);

//config admin
var Admin = require('./models/Admin');
passport.use(new LocalStrategy(Admin.authenticate()));
passport.serializeUser(Admin.serializeUser());
passport.deserializeUser(Admin.deserializeUser());

admin.js:

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

var AdminSchema = new mongoose.Schema({
    username: String,
    password: String
});

AdminSchema.plugin(passportLocalMongoose);

module.export = mongoose.model('Admin', AdminSchema);

It's a typo. You have to use module.exports and not module.export in admin.js.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM