KARTHIKEYAN.A
KARTHIKEYAN.A

Reputation: 20088

Unable to change error response code in feathers.js

I'm use authentication service for login validation., in that i want to Unauthorized (401) response code to 200 and message should be same.

my authentication service is

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();
        }
      ]
    }
  });

my middle ware code is

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);
    }
  });
});

my response message is

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

but i want

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

Upvotes: 1

Views: 1382

Answers (2)

Daff
Daff

Reputation: 44215

Another option would be to swallow the error by setting hook.result in the error handler which will automatically return a successful HTTP code:

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

Upvotes: 1

KARTHIKEYAN.A
KARTHIKEYAN.A

Reputation: 20088

Finally i found the solution., we need to change the response code in hook error method.

for error response code change:

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();
      }]
    }
  });

for result response code change:

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));

Upvotes: 1

Related Questions