fenichelar/ember-simple-auth-token

Server response body is swallowed if code is not OK

happycollision opened this issue · 4 comments

Pretty sure this was introduced at commit 7558b36

Since v4.0.0, if a server responds with something like this (with a non-ok response code):

{errors: [{
  status: 401,
  title: 'no user exists with that email and password combination',
}]}

The errors array disappears and the only thing available in the catch block of the call to authenticate() is a vanilla error where message is "Unauthorized".

Given the following:

this.get('session').authenticate('authenticator:jwt', badCredentials)
  .catch(e => console.log(e));

3.x console:

{
  errors: [
    {
      status: 401,
      title: 'no user exists with that email and password combination',
    }
  ]
}

Post 4.0.0 console:

Error: Unauthorized
    at then.response (token.js:92)
    at tryCatcher (rsvp.js:215)
    at invokeCallback (rsvp.js:393)
    at publish (rsvp.js:379)
    at rsvp.js:14
    at invoke (backburner.js:205)
    at Queue.flush (backburner.js:125)
    at DeferredActionQueues.flush (backburner.js:278)
    at Backburner.end (backburner.js:410)
    at Backburner._boundAutorunEnd (backburner.js:372)

@happycollision How does that look? In your example, the console output would be:

{
   "statusText": "Unauthorized",
   "status": 401,
   "json": {
      "errors": [
         {
            "status": 401,
            "title": "no user exists with that email and password combination"
         }
      ]
   }
}

That works for me just fine.

But are there scenarios where the server might not be sending JSON? Perhaps the error messages will be in plain text, etc. That discussion is up to you and your fellow collaborators.

In any case, thanks for that amazingly fast response!

Yeah, I thought about. It was kind of messy so I figured it wasn't worth it but you are right. I added support for both: 32568ba

JSON response:

{
  statusText: "Unauthorized",
   status: 401,
   text: "{"errors":[{"status":401,"title":"no user exists with that email and password combination"}]}",
   json: {
      "errors": [
         {
            "status": 401,
            "title": "no user exists with that email and password combination"
         }
      ]
   }
}

Text response:

{
  statusText: "Unauthorized",
   status: 401,
   text: "no user exists with that email and password combination"
}