2017-05-23 20 views
0

ユーザーログインの認証にはJWT'sが使用されていますが、/authルートでPOSTを実行するときには常にエラーが発生します。ノードJS TypeError:シークレットは文字列またはバッファーでなければなりません

(node:3385) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: secret must be a string or buffer

(node:3385) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

は、ここに私のコードの答えのための

app.post('/auth', function (req, res, next) { 
    var username = req.body.username; 
    var password = req.body.password; 

    User.findOne({ 
     where: {username: username} 
    }).then(function (user) { 
     if(user){ 
      bcrypt.compare(req.body.password, user.password).then(function (passcheck) { 
       if(passcheck){ 
        var jwtUser = { 
         username: user.username, 
         name: user.name, 
         score: user.score, 
         accesslevel: "all" 
        }; 

        var token = jwt.sign(jwtUser, app.get('superSecretString'), { 
         expiresIn: 1440 //expires in 24 hours 
        }); 

        //callback(token); 

        res.json({ 
         success: true, 
         message: 'Here´s your token.', 
         token: token 
        }); 

        /* 
        var resp = {success: true, message: 'Heres your token.', token: token}; 
        response.write(JSON.stringify(resp)); 
        */ 
       }else{ 
        res.status(401).json({ 
         success: false, 
         message: 'Authentification failed. Password or Username wrong' 
        }); 
       } 
      }); 
     }else{ 
      res.status(401).json({ 
       success: false, 
       message: 'Authentification failed.' 
      }); 
     } 
    }).catch(next); 
}); 

おかげです。

+2

イムを追加します。私はこのapp.get( 'superSecretString')からエラーをチェックすることにします。 –

+0

@MykolaBorysyuk thx、私はconfig.js "module.exports"の "s"を忘れてしまった – pache

答えて

1

問題は、設定ファイルで秘密と宣言されていないことです。 あなたが別の方法でそれを行うことができます

const token = jwt.sign(user, '123456', { 
        expiresIn: 60 * 24 // expires in 24 hours 
       }); 

app.get('superSecretString')を削除し、jwt.sign正しいパラメータに渡す必要が考え'123456'

関連する問題