Issueing a POST and receiving no id back


#1

I’m implementing an app that uses our own API. In this API, authentication is based on a POST query to a certain URL with basic authentication that returns a couple of tokens that will be later used to “sign” all the requests.

I don’t seem to be able to map this request to any model because ember keeps on complaining for the lack of id in the ember model and in the response from the server.

I’ve tried workarounding this at many stages so that I can benefit of having this as a service and inject the tokens in all the other adapters but I haven’t been successful “yet”.

So far I tried:

  • Writing a serializer and use the normalizeCreateRecordResponse to add a fake id.

  • Use normalize to normalize the response.

  • Writing a createRecord hook in the adapter and use the super function to issue the query and return a JSON with a fake id.

I’m using a RESTAdapter and a JSONSerializer but so far nothing really worked as I expected and I just collected a set of failed assertions and exceptions on the promises.

Which would be the right approach to handle this efficiently?.

Thank you very much for your help. I can supply more information if needed and I’m really questioning using ember-data due to it being very strict on the API format.


#2

You should be implementing Ember Service for your scenario.

Please refer

https://emberigniter.com/implementing-authentication-with-ember-services/

or better use ember-simple-auth

Please refer

https://emberigniter.com/real-world-authentication-with-ember-simple-auth/


#3

Hi Elisha,

thanks for your help. Very nice reading. Actually I had set up a similar approach to the first link.

However, one of my concepts is that I wanted to avoid writing the ajax queries as much as possible and use the adapters and serializers from ember. This is why I set up a model for these tokens and called a createRecord on it. This createRecord is the one that spoils my concept actually :frowning: because even though I customise the headers and host, the response from the server doesn’t contain an id on the JSON.

I tried adding a “fake” id on the payload via the serialiser in the normalizeCreateRecordResponse hook but that doesn’t seem to enable me to pass all the assertions either. I usually get an assertion saying that the record has been created on the server (since HTTP response is code 200) but that there’s no id set neither in my local model nor in the server.

Do you think my approach can match with ember at some point?. Do you foresee a way that this can be overpassed using a model for the request of the tokens as I’d like to do?.

Thanks again for your help.


#4

Please refer to a similar discussion regarding authentication in emberjs.

Using ember-simple-auth relieves you from doing ajax stuff.

IMHO, you’ll avoid lot of headaches when you follow battle-tested approaches. Hence, I would recommend ember-simple-auth for your use case.