Passport.js .authenticate()抛出TypeError:未定义不是函数 [英] Passport.js .authenticate() throwing TypeError: undefined is not a function

查看:34
本文介绍了Passport.js .authenticate()抛出TypeError:未定义不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是通行证的新手(并且通常是结点),并且一直在使用本教程(

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

我检查了package.json,所有相关的库似乎都已安装.

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

这是相关的代码.

来自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);

推荐答案

这是一个错字.您必须在admin.js中使用 module.exports 而不是 module.export .

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

这篇关于Passport.js .authenticate()抛出TypeError:未定义不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆