2017-02-28 78 views
0

我試圖建立護照本地登錄,但提交表單時沒有做任何事情。Node.js的app.post護照本地註冊形式不工作

我app.js代碼:

var express = require('express'); 

var app = express(); 
app.use(express.static(__dirname + '/public')); 
var credentials = require('./credentials.js'); 
app.use(require('cookie-parser')(credentials.cookieSecret)); 
app.use(require('body-parser').urlencoded({extended: false})); 
var session = require('express-session'); 
app.use(session({ 
    resave: false, 
    saveUninitialized: true, 
    secret: 'keyboard cat' 
})); 
var passport = require('passport'); 
app.use(passport.initialize()); 
app.use(passport.session()); 

app.disable('x-powered-by'); 

var handlebars = require('express-handlebars').create({defaultLayout:'main'}); 

app.engine('handlebars', handlebars.engine); 
app.set('view engine', 'handlebars'); 

app.set('port', process.env.PORT || 3000); 
app.post('/login', function(req, res){console.log("body parsing", req.body)}); 

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

在爲快速登入/登錄的HTML表單:

<form action="/login" method="post"> 
    <div> 
     <label>Username:</label> 
     <input type="text" name="username"/> 
    </div> 
    <div> 
     <label>Password:</label> 
     <input type="password" name="password"/> 
    </div> 
    <div> 
     <button type="submit">Submit</button> 
    </div> 
</form> 

當我填寫用戶名和密碼,沒有打印出到從控制檯發佈請求。我有什麼錯誤?

+0

請查看http://passportjs.org/docs/username-password – unlimit

回答

0

我發現,除去該AMP js腳本解決問題訪問數據。出於某種原因,它打破了發佈請求。

<script async src="https://cdn.ampproject.org/v0.js"></script> 
1

您必須序列化和反序列化的用戶信息進行認證的用戶。

這裏是和榜樣。

services.js

const passport = require('passport'); 
const LocalStrategy = require('passport-local').Strategy; 
const User = require('../models/user'); 

//serialize and deserialize 
passport.serializeUser(function(user, done) { 
    done(null, user._id); 
}); 

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

//middleware 
passport.use('local-login', new LocalStrategy({ 
    usernameField: 'email', 
    password: 'password', 
    passReqToCallback: true 
}, (req, email, password, done) => { 
    User.findOne({ email: email.toLowerCase() }, function(err, user) { 
     if (err) return done(err); 

     if (!user) { 
      return done(null, false, req.flash('loginMessage', 'No user has been found')); 
     } 

     if (!user.comparePassword(password)) { 
      return done(null, false, req.flash('loginMessage', 'Oops! Wrong Password.')); 
     } 
     return done(null, user); 
    }); 
})); 

//custom function to validate 
exports.isAuthenticated = function(req, res, next) { 
    if (req.isAuthenticated()) { 
     return next(); 
    } 
    res.redirect('/login'); 
} 

,一旦你做的序列化,你必須的過程實際上也適用於路由器。即使用戶的詳細信息進入護照。

在你的路由器的文件:

const passportConfig = require('services.js'); 

然後,你可以簡單地通過req.user

+0

我曾嘗試複製護照文檔中的代碼,但我認爲我的問題還沒有出現。我在調試中刪除了大部分代碼,我想我的觀點是爲什麼在填寫並提交表單時,此行不顯示任何內容? app.post('/ login',function(req,res){console.log(「body parsing」,req.body)}); –