2016-12-04 113 views
1

我是passport.js的新手。 我一直在關注以下博客註冊用戶快遞/護照 - passport.authenticate不是功能

https://scotch.io/tutorials/easy-node-authentication-setup-and-local

但每次我運行服務器和時間我加載/register頁面它顯示了上述錯誤

TypeError: passport.authenticate is not a function at module.exports (/home/punit/Documents/****/routes/index.js:10:34) at Layer.handle [as handle_request] (/home/punit/Documents/****/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/home/punit/Documents/****/node_modules/express/lib/router/index.js:312:13) at /home/punit/Documents/****/node_modules/express/lib/router/index.js:280:7 at Function.process_params (/home/punit/Documents/****/node_modules/express/lib/router/index.js:330:12) at next (/home/punit/Documents/****/node_modules/express/lib/router/index.js:271:10) at /home/punit/Documents/****/node_modules/connect-flash/lib/flash.js:21:5 at Layer.handle [as handle_request] (/home/punit/Documents/****/node_modules/express/lib/router/layer.js:95:5)

我有檢查變量將填充到/routes/index.js文件中。

var mongoose = require('mongoose'); 
var passport = require('passport'); 
var flash = require('connect-flash'); 
var session = require('express-session'); 

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

mongoose.connect("localhost:27017/****"); 

require('./models/strategies')(passport) 

var app = express(); 

會話初始化是如下

app.use(session({ 
    secret: '*******', 
    resave: true, 
    saveUninitialized: false 
})); // session secret 
app.use(passport.initialize()); 
app.use(passport.session()); // persistent login sessions 
app.use(flash()); // use connect-flash for flash messages stored in session 


app.use('/', routes); 
app.use('/users', users); 
app.use('/register', routes); 

require('./routes/index')(app, passport) 

的strategies.js文件是如下

var LocalStrategy = require('passport-local').Strategy 
var Profile = require('./profile.js') 

var ex = function (passport) { 

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

    passport.deserializeUser(function (id, done) { 
    Profile.findById(_id, function (err, user) { 
     done(err, user) 
    }); 
    }); 

    passport.use('register', new LocalStrategy({ 
    usernameField: 'email', 
    passwordField: 'password', 
    passReqToCallback: true 
    }, 
    function(req, mobile, password, done) { 
    process.nextTick(function() { 
     return done(null, false); 

    }); 
    })) 
} 

console.log(ex) 

module.exports = ex; 

和路由/ index.js文件如下

module.exports = function (app, passport) { 
    /* GET home page. */ 
    app.get('/login', function(req, res, next) { 
    res.render('index', { title: 'Express' }); 
    }); 
    app.get('/register', function(req, res, next) { 
    res.render('register', { title: 'Express' }); 
    }); 

    app.post('/register', passport.authenticate('register', { 
    successRedirect: '/profile', 
    faliureRedirect: '/register', 
    faliureFlash: true // allows flash message 
    }), function(req, res, err) { 
    console.log(req) 
    }) 

} 

任何形式的幫助將是一個ppreciated。

回答

1

您應該使用路由文件路由器或通過app

只有一個會工作。

//app.use('/', routes); 
//app.use('/users', users); 
//app.use('/register', routes); 

require('./routes/index')(app, passport) 

這樣護照對象將在路徑文件中可用。

0

可能不是最好的答案,但應該在你的情況下工作,改變strategies.js文件:

var LocalStrategy = require('passport-local').Strategy 
var Profile = require('./profile.js') 

var ex = function (passport) { 
return (
    passport.serializeUser(function (user, done) { 
    done(null, user.id) 
    }); 

    passport.deserializeUser(function (id, done) { 
    Profile.findById(_id, function (err, user) { 
     done(err, user) 
    }); 
    }); 

    passport.use('register', new LocalStrategy({ 
    usernameField: 'email', 
    passwordField: 'password', 
    passReqToCallback: true 
    }, 
    function(req, mobile, password, done) { 
    process.nextTick(function() { 
     return done(null, false); 

    }); 
    })) 
) 
} 

console.log(ex) 

module.exports = ex;