Skip to content
This repository was archived by the owner on Mar 22, 2022. It is now read-only.

REST Middleware breaks local auth #262

Closed
petermikitsh opened this issue Aug 6, 2016 · 5 comments
Closed

REST Middleware breaks local auth #262

petermikitsh opened this issue Aug 6, 2016 · 5 comments

Comments

@petermikitsh
Copy link
Contributor

Using an empty REST middleware that calls next() breaks local authentication:

  .configure(rest(function (req, res, next) {
    next();
  }))

vs no middleware:

  .configure(rest())

When the middleware is added, a 404 response is returned. Response content is: Cannot POST /auth/local.

screen shot 2016-08-06 at 2 16 09 pm

@daffl
Copy link
Member

daffl commented Aug 6, 2016

Is what you want to accomplish the same thing as documented in http://docs.feathersjs.com/rest/readme.html#customizing-the-response-format?

The default formatter can be found in https://github.com/feathersjs/feathers-rest/blob/master/src/index.js#L6.

@petermikitsh
Copy link
Contributor Author

Yeah, my use case is I need a special endpoint for one of my services that returns an XML document.

I tried defining a formatter right before `app.use('myCustomEndpoint', { get(id) { ... }});`` e.g.,

app
  .config(function (req, res, next) { ... })
  .use('myCustomEndpoint', { ... });

But that wasn't working for me.

@petermikitsh
Copy link
Contributor Author

Okay, that helped, thanks! I ended up writing my own formatter:

function (req, res, next) {
  if (!res.data) {
    next();
  }
  if (req && req.originalUrl && req.originalUrl.indexOf('myCustomEndpoint') === 0) {
    res.set('Content-Type', 'text/xml');
    res.end(res.data.xml);
  } else {
    res.format({
      'application/json': function () {
        res.json(res.data);
      }
    });
  }
};

A little tricky since it works similarly, but not exactly like, express middleware.

@daffl
Copy link
Member

daffl commented Aug 6, 2016

It is an Express middleware, I don't think there is any difference. I'm wondering if in your case you really just want to register a formatter after the service (so that the default never gets hit):

app.use('/mycustomservice', myCustomService, function(res, res) {
  res.set('Content-Type', 'text/xml');
  res.end(res.data.xml);
});

@petermikitsh
Copy link
Contributor Author

Yeah, that is much more what I was looking for since I didn't need to change the global formatter. Thanks again!

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants