2015-02-08 56 views
0

我是Node.js的新手。嘗試設置用戶帳戶創建並使用Passport登錄。我無法工作。雖然我使用現有用戶登錄並通過,但我收到了未經授權的消息。到目前爲止我的代碼:Node.js使用Express和Mongoose設置Passport的問題

var express = require('express'); 
var http = require('http'); 
var path = require('path'); 
var request = require('request'); 
var app = express(); 
var passport = require('passport'); 
var LocalStrategy = require('passport-local').Strategy; 
var MongoClient = require('mongodb').MongoClient; 

app.set('port', process.env.PORT || 3000); 
app.set('views', path.join(__dirname, 'views')); 
app.engine('html', require('ejs').renderFile); 

app.use(express.favicon()); 
app.use(express.logger('dev')); 
app.use(express.json()); 
app.use(express.urlencoded()); 
app.use(express.methodOverride()); 
app.use(passport.initialize()); 
app.use(passport.session()); 
app.use(app.router); 
app.use(express.static(path.join(__dirname, 'public'))); 
app.use(express.bodyParser()); 
app.use(app.router); 
app.enable('trust proxy'); 

if ('development' == app.get('env')) { 
} 

var httpserver = http.createServer(app).listen(app.get('port'), function(){ 
    console.log('Express server listening on port ' + app.get('port')); 
}); 

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

/* Database has _id field but i guess i don't need to add anything here?*/ 

var Accoun = new Schema({ 
    username: String, 
    password: String 
}); 

/* Database collection is called 'users'*/ 
Accoun.plugin(passportLocalMongoose); 
var Account = mongoose.model('users', Accoun); 

passport.use(new LocalStrategy(Account.authenticate())); 
passport.serializeUser(Account.serializeUser()); 
passport.deserializeUser(Account.deserializeUser()); 

mongoose.connect('xxxmyconnectionstring'); 

app.get('/', function(req, res) { 
    res.render('login.html'); 
}); 


    app.post('/login', passport.authenticate('local'), function(req, res) { 
     res.redirect('/myteam.html'); 
    }); 

我需要一些專家的幫助......

問候,

回答

1

我相信你需要一個successRedirect和failureRedirect在app.post。

+0

我終於使用了一個已經實現的登錄應用程序:https://github.com/braitsch/node-login謝謝 – Egidi 2015-02-11 12:55:22

相關問題