2015-10-07 27 views
1

所以試圖爲用戶註冊一個本地策略,很簡單的前端,四個字段;用戶名,電子郵件,密碼(然後再次輸入密碼,因爲它在表格中)。我知道後的作品,下面的信息作爲它的一部分發送passport.authenticate去世無聲無息

username:user 
email:[email protected] 
password:IAMPASSWORD 
passwordConfirm:IAMPASSWORD 

然後我用我的理解是一個非常簡單passport.authenticate(OK,我覺得應該是簡單的,但顯然不是THAT簡單)。

var cfgWebPage = require('../config/webpage.js') 


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


    //This is signup 
    app.get('/signup', function(req, res) { 

     // render the page and pass in any flash data if it exists 
     res.render('signup.ejs', { title: 'Sign up to our service' , loginUrl: cfgWebPage.loginUrl, trackingID: cfgWebPage.googleTracking.trackingID, message: req.flash('signupMessage') }); 
    }); 
    // process the signup form 
    app.post('/signup', passport.authenticate('local-signup', { 
     successRedirect : '/profile', // redirect to the secure profile section 
     failureRedirect : '/signup', // redirect back to the signup page if there is an error 
     failureFlash : true // allow flash messages 
    })); 
} 

,它肯定得這麼遠,(改變了它的的console.log消息只是爲了測試)。

我得到的迴應從它做了302回服務器,它的行爲就好像它已經失敗,但沒有在控制檯中。

// required for passport 
app.use(session({secret: 'Thisisnottherealone', 
    saveUninitialized: true, 
    resave: true 
})); // 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 
//Configuring the passports 
require('./config/passport')(passport); 


var routes = require('./routes/index')(app, passport); 
  1. 所以它找到的護照,我猜 - 會,預計到 抱怨並非如此。
  2. 似乎調用身份驗證 - 沒有錯誤有

我試圖更改passport.js文件的東西(包含登錄了我的護照設置),但它似乎永遠不會走到這一步,似乎在數據庫打開後的某個時間死亡。 這裏是passport.js文件(從配置)

// config/passport.js 

// load up the user model 
var LocalStrategy = require('passport-local').Strategy; 

// load up the user model 
var mysql = require('mysql'); 
var bcrypt = require('bcrypt-nodejs'); 
var dbconfig = require('./database'); 
var sqlConnection = mysql.createConnection(dbconfig.sqlConnection); 


sqlConnection.query('USE ' + dbconfig.sqlDatabase); 

// expose this function to our app using module.exports 
module.exports = function(passport, app) { 

    // ========================================================================= 
    // passport session setup ================================================== 
    // ========================================================================= 
    // required for persistent login sessions 
    // passport needs ability to serialize and unserialize users out of session 

    // used to serialize the user for the session 
    passport.serializeUser(function(user, done) { 
     done(null, user.id); 
    }); 

    // used to deserialize the user 
    passport.deserializeUser(function(id, done) { 
//  User.findById(id, function(err, user) { 
//   done(err, user); 
//  }); 
     passport.deserializeUser(function(id, done) { 
      console.log('deserialising'); 
      sqlConnection.query("SELECT * FROM users WHERE id = ? ",[id], function(err, rows){ 
       done(err, rows[0]); 
      }); 
     }); 
    }); 

    // ========================================================================= 
    // LOCAL SIGNUP ============================================================ 
    // ========================================================================= 
    // we are using named strategies since we have one for login and one for signup 
    // by default, if there was no name, it would just be called 'local' 

    passport.use(
     'local-signup', 
     new LocalStrategy({ 
       // by default, local strategy uses username and password, we will override with email 
       emailField : 'email', 
       usernameField : 'username', 
       passwordField : 'password', 
       passReqToCallback : true // allows us to pass back the entire request to the callback 
      }, 
      function(req, username, email, password, done) { 
       // find a user whose email is the same as the forms email 
       // we are checking to see if the user trying to login already exists 
       console.log("Calling database!"); 
       sqlConnection.query("SELECT * FROM users WHERE username = ?",[username], function(err, rows) { 
        if (err) 
         return done(err); 
        if (rows.length) { 
         return done(null, false, req.flash('signupMessage', 'That username is already taken.')); 
        } else { 
         // if there is no user with that username 
         // create the user 
         var newUserMysql = { 
          username: username, 
          email: email, 
          password: bcrypt.hashSync(password, null, null) // use the generateHash function in our user model 
         }; 

         var insertQuery = "INSERT INTO users (username, email, password) values (?,?)"; 

         sqlConnection.query(insertQuery,[newUserMysql.username, newUserMysql.password],function(err, rows) { 
          newUserMysql.id = rows.insertId; 

          return done(null, newUserMysql); 
         }); 
        } 
       }); 
      }) 
    ); 

}; 

任何建議,其中的問題,或者調試它最簡單的方法?

+0

到底====例的問題是相當簡單的。 – vrghost

回答

1

最終的問題是簡單的,並沒有任何關係的護照。

我已經錯過了進入的數據進行解碼。 加入以下內容,讓你的叔叔(發送passport.authenticate沒有任何用戶信息使護照傷心,並使其全部沉默)死亡。

var bodyParser = require('body-parser'); 



module.exports = function(app, passport) { 
    var urlencodedParser = bodyParser.urlencoded({extended: false}) 
    /* GET home page. */ 

API調用=====

app.post('/login', urlencodedParser, passport.authenticate('local-login', { 
     successRedirect : '/profile', // redirect to the secure profile section 
     failureRedirect : '/login', // redirect back to the signup page if there is an error 
     failureFlash : true // allow flash messages 
    })); 
+0

我們在哪裏傳遞urlencodedParser變量? –

+0

passport.authenticate對我來說是無聲無息的 –

+0

您按照上述方式啓動它,然後將其作爲函數調用的一部分傳遞給它。我會舉一個例子。 – vrghost