Coloquent icon indicating copy to clipboard operation
Coloquent copied to clipboard

How to use with Authentication

Open spawnia opened this issue 6 years ago • 8 comments

Hi, i want to use Coloquent in my project - the syntax and ease of use look really nice.

My API requires Authentication via an Access Token sent in the Header. While looking through the source code of Model, i noticed that you are not exposing Axios options to the child class.

Am i missing something, is there an easy way to do this? If not, are there plans of exposing such configuration?

Thank you

spawnia avatar Feb 20 '18 07:02 spawnia

I'll release a fix tonight, it will basically come down to that you can inject your own AxiosInstance with a method setApiClient on Model.

DavidDuwaer avatar Feb 20 '18 09:02 DavidDuwaer

Really cool, i will take a look once you have it and happily test it!

spawnia avatar Feb 20 '18 09:02 spawnia

Are any changes to this issue?

kuzyk avatar Feb 23 '18 07:02 kuzyk

I've addressed the issue and released the solution.

To protect future backward-compatibility, I first had to isolate Coloquent from the HTTP client it uses by wrapping the HTTP client (and its promise, and its response) in interfaces. You can now obtain the AxiosInstance from Model by doing instanceOfModel.getHttpClient().getImplementingClient() (if using TypeScript you might wanna cast it to AxiosInstance). You can set its configuration in that object's .default property.

You can even inject any HTTP client now with instanceOfModel.setHttpClient, provided that you implement Coloquent's HttpClient, HttpPromise and HttpResponse interfaces for it.

DavidDuwaer avatar Feb 23 '18 09:02 DavidDuwaer

@spawnia @kuzyk Let me know if this solves your problem!

DavidDuwaer avatar Feb 23 '18 18:02 DavidDuwaer

Thank you for taking the time to work on this. Your solution does work, and it offers a lot of flexiblity.

However, i think authentication via JWT or similar mechanisms is such a common use case and should probably be a first-class configuration option. It would be useful to have an example configuration, probably via a shared Model class, overriding the default configuration.

spawnia avatar Apr 01 '18 12:04 spawnia

I agree that it would be very useful to set the Authorization header (or some other custom headers) without needing to reimplement the HttpClient interface.

I currently can't see a way of setting the HTTP client at the collection fetching level? e.g. how would I make Artist.get() use the custom HttpClient?

andrewn avatar Apr 24 '18 08:04 andrewn

(Update to the new version (v1.0.2), the exposed HttpClient was not static on the Model class, causing a new client instance to be used for every model instance; this is now fixed.)

You can set the HTTP client headers by getting the "implementing client" instance, and setting the proeprty there. Since Coloquent by default has Axios, you can do it like this:

let axiosInstance: AxiosInstance = YourModel.getHttpClient().getImplementingClient();
axiosInstance.defaults.headers.authentication = 'someAuthenticationHeader5636rt3';

After setting this, the authorization header is set for all queries. We can make an API so you can set default headers directly on Model, I'm open to pull requests. Would be nice if the configuration methods are fluent as well (e.g. Model.setDefaultHeader('foo', 'value').setDefaultHeader('bar', 'value')). Also, do you need to be able to set it for specific requests? I can imagine a .header operator to be useful in a query.

DavidDuwaer avatar Apr 27 '18 14:04 DavidDuwaer