2017-11-07 8 views
0

私はログイン検証に認証サービスを使用していますが、私は無許可(401)応答コードを200にしてメッセージを同じにします。feathers.jsのエラー応答コードを変更できません

私の認証サービスがある

app.service('authentication').hooks({ 
    before: { 
     create: [ 
     authentication.hooks.authenticate(config.strategies), 
     function (hook) { 
     hook.params.payload = { 
      userId: hook.params.user.userId, 
      accountId: hook.params.user.accountId 
      }; 
      return Promise.resolve(hook); 
     } 
     ], 
     remove: [ 
     authentication.hooks.authenticate('jwt') 
     ] 
    }, 
    after: { 
     create: [ function(hook,next){ 
      hook.result.statusCode = 201; 
      hook.result.authentication = "user login successful"; 
      next(); 
     } 
     ] 
    } 
    }); 

私のミドルウェア・コードがある

app.use(function(err, req, res, next) { 
    res.status(err.status || 200); 

    res.format({ 
    'text/html': function(){ 
     // Probably render a nice error page here 
     return res.send(err); 
    }, 

    'application/json': function(){ 
     res.json(err); 
    }, 

    'text/plain': function(){ 
     res.send(err.message); 
    } 
    }); 
}); 

私の応答メッセージが

{ 
    "name": "NotAuthenticated", 
    "message": "Invalid login", 
    "code": 401, 
    "className": "not-authenticated", 
    "data": { 
     "message": "Invalid login" 
    }, 
    "errors": {} 
} 

ですが、私は

{ 
    "name": "NotAuthenticated", 
    "message": "Invalid login", 
    "code": 200, 
    "className": "not-authenticated", 
    "data": { 
     "message": "Invalid login" 
    }, 
    "errors": {} 
} 
をしたいです210
+0

'res.status(err.status === 401? 200:(err.status || 200) '? – Will

+0

その動作していない@wbadart –

答えて

0

最後に解決策を見つけました。フックエラーメソッドで応答コードを変更する必要があります。

エラー応答コードの変更のために:結果応答コード変更のための

app.service('authentication').hooks({ 
    before: { 
     create: [ 
     authentication.hooks.authenticate(config.strategies), 
     function (hook) { 
     hook.params.payload = { 
      userId: hook.params.user.userId, 
      accountId: hook.params.user.accountId 
      }; 
      return Promise.resolve(hook); 
     } 
     ], 
     remove: [ 
     authentication.hooks.authenticate('jwt') 
     ] 
    }, 
    after: { 
     create: [ function(hook,next){ 
      hook.result.code = 200; 
      hook.result.authentication = "user login successful"; 
      next(); 
     } 
     ] 
    }, 
    error: { 
     create: [function(hook, next){ 
     hook.error.code = 200; 
     next(); 
     }] 
    } 
    }); 

function restFormatter(req, res) { 
    res.format({ 
    'application/json': function() { 
     const data = res.data; 
     res.status(data.__status || 200); 
     res.json(data); 
    } 
    }); 
} 

app.configure(rest(restFormatter)); 
0

別のオプションは、errorハンドラでhook.resultを設定することで、エラーを飲み込むことであろう正常なHTTPコードを自動的に返します:

app.service('authentication').hooks({ 
    error: { 
    create: [ function(hook, next){ 
     hook.result = { authentication = "user login successful" }; 
    } ] 
    } 
}); 
関連する問題