2017-11-04 10 views
1

ExpressとPassportを使用して小さなログインアプリケーションを作成しました。しかし、私は特定のuserprofileにユーザーを誘導することはできないようです。起こるべきことは、ユーザーがHTMLフォームをいっぱいにして、自分のプロフィールページにリダイレクトする必要があるということです(簡単にするため、そのページは他のユーザーページとタイトルが異なるだけです)。私は自分のユーザールートにこれを持っています。 (routes/users.js)ExpressでPOSTでパラメータをPassportで渡す

router.get('/userprofile', authenticationMiddleware(), function (req, 
res, next) { 
    res.render('userprofile'); 
}); 


router.post('/login', passport.authenticate('local-login', { 
     successRedirect: '/users/userprofile', 
     failureRedirect: '/users/login', 
     failureFlash: true 
})); 

私のpassport.js設定ファイルにはこれがあります。私がする必要がどのような

passport.use('local-login', new LocalStrategy({ 
    usernameField: 'username', 
    passwordField: 'password', 
    passReqToCallback: true 
}, function (req, username, password, done) { 
    db.pool.getConnection(function (err, connection) { 
     if(err) throw err; 
     var query = connection.query('SELECT * from users WHERE username = ?', username, function (err, rows) { 
      if(err) return done(err); 
      if(!rows.length) { 
       return done(null, false, req.flash('loginMessage', 'No User Found')); 
      } 
      //Comparing passwords using bcrypt 
      bcrypt.compare(password, rows[0].password, function(error, res){ 
       if(res){ 
        return done(null, rows[0]); 
       } else { 
        return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); 
       } 
      }); 
     }); 
    }); 
})); 

は、フォームからユーザー名を取得し、passport.authenticateでsuccessRedirect属性に渡し、その後、私は「/ユーザー/などのルートパラメータを追加getメソッドを変更することができます: username 'を表示し、' username 'ビューを表示します。

どうすればいいですか?

EDIT:

私はこのような何かをしました。

router.get('/userprofile/:username', authenticationMiddleware(), function (req, res, next) { 
    res.render('userprofile', { 
     username: req.params.username, 
     title: 'Welcome, '+ req.params.username 
    }); 
}); 


router.post('/login', function (req, res, next) { //Testing callback. 
    console.log("Username is: " + req.body.username); 
    passport.authenticate('local-login', { 
     successRedirect: '/users/userprofile/' + req.body.username, 
     failureRedirect: '/users/login', 
     failureFlash: true 
    })(req, res);  
    next(); 
}); 

これはいくつかの試みで機能します。ただし、時々このエラーが返されます。

POST /users/login 404 43.741 ms - 5226 /home/dasun/WebstormProjects/schoolentry/node_modules/passport/lib/middleware/authenticate.js:249 
     if (err) { return next(err)}; 

どうすればこのエラーを取り除くことができますか?

ありがとうございました。

+0

私が書く必要がありますか? –

+0

@GibryonBhojrajは返信いただきありがとうございます。私は私の質問の** EDIT **セクションにいくつかの編集を追加しました。リクエストにユーザー名を追加することもできますが、** EDIT **セクションに記載されているエラーが返されます。これはときどき発生します。時にはうまくいくこともありますが、時にはエラーが発生することがあります。 – dasunpubudumal

答えて

0

答えを見つけました。私は/loginルートのPOSTメソッドでnextが見逃されました。

つまり、あなたがそれを渡すのではなく、あなたの/ USERPROFILEルートでreq.userを使用することができ

router.post('/login', function (req, res, next) { //Testing callback. 
console.log("Username is: " + req.body.username); 
passport.authenticate('local-login', { 
    successRedirect: '/users/userprofile/' + req.body.username, 
    failureRedirect: '/users/login', 
    failureFlash: true 
})(req, res, next); 
}); 
関連する問題