2017-06-13 94 views
0

我試圖使用護照包進行身份驗證。在passport-authenticate回調函數中,req.login函數會拋出一個錯誤:TypeError: secret must be a string or buffer。我檢查了傳遞給函數的用戶參數,看起來是正確的(下面打印)。我不知道還有什麼可能是錯誤的根源。我不確定這個錯誤指的是什麼祕密。如果您需要更多信息,請告訴我。 - Nodejs新手。nodejs:TypeError:祕密必須是字符串或緩衝區

router.post('/login', function(req, res, next) { 
    passport.authenticate('local', function(err, user, info) { 
    if (err) { 
     return next(err); 
    } 
    if (!user) { 
     return res.status(401).json({ 
     err: info 
     }); 
    } 
    req.login(user, function(err) { 
     if (err) { 
      console.log(`user = ${user}`); 
      console.log(`${err}`); 
      return next(err); 
     // console.log(`${err}`); 
     // return res.status(500).json({ 
     // err: 'Could not log in user' 
     // }); 
     } 

     var token = Verify.getToken(user); 
       res.status(200).json({ 
     status: 'Login successful!', 
     success: true, 
     token: token 
     }); 
    }); 
    })(req,res,next); 
}); 

輸出:

user = { _id: 593f8110dd56731d6d1ad50a, 
    salt: 'd8bf193acbea5d2ee50eecb88c3b8fd3fbc2316ced558270aa5699558e251a26', 
    hash: '3fc0687f862fd79d88f03c7feb448a1946dfe3bbf33229272855f3ad9c04aa20d548e56c17cd4e37538e4b9c570a19b393768100ec8f21707a42d239dd25ebea18c5925810ee5e71f66645c5ff938ad9230f07ef42f4c890c923f45d2d00754ad0f82d10c0bf03dfb345711f5cd081194c186b18b60698a82776971f5839435b3a2291a8df8c3118b050ed9462f64589b42fde76b5a59749e9722a86ef0f8dd1e0417f691bae21c31a3500cadeaf1aa0901282b42a383581b3c15fb909b54a7bb99cea67273e69e271bfb7edc156e5d0c4aa1da9d50ad8003125ccca772f95e01cc84965710e199013fb2a50092afa9f2af0fe4ebd391f8056aad3f726bee4f09218737afe236c392c3dda89a15b337f5510395e38b77bbbb60380dcb7822142f6f8f965f678f9b3bdfd9fe7752c50738805a45277f512ed6486bdfc4f1837d0ab656b6838e663406d1ca3dc00e4a36b9917c8122bb252578deb402418dc7509493ffd5992947c46a804a43e1d84b161504b7bb615a77155a79e0e97bed88a8bbeedd9625216bf3554b507e0f705c11d0e0624ec9102c32fb629dcd96e1ef15bbab8bd55191bac80c6c3d8a84facf1f26f6884b236cdc2224add24acc253816ad53bfba3adb7da1aa10aaafcc53a810f7f826fadc5cdab1bd6c5bf4bd1e6fb898dbe359445a3243444cf93b9e6b991d149956951855f2eca0e0bc91111810825', 
    username: 'muppala', 
    __v: 0, 
    admin: false } 
TypeError: secret must be a string or buffer 
POST /users/login 500 465.600 ms - 58 

回答

0

的錯誤發生,因爲您的config.js的祕密密鑰不能被訪問。確保它已正確導入到verify.js中。

相關問題