SpoonX / aurelia-api

A simple convenience wrapper around aurelia-fetch-client
http://aurelia-api.spoonx.org
MIT License
72 stars 21 forks source link

How do change the value of a header after the initial endpoint configuration? #192

Closed delebash closed 7 years ago

delebash commented 7 years ago

This question may be related to https://github.com/SpoonX/aurelia-api/issues/185

I have setup two endpoints auth and api. The auth endpoint returns a session token which I need to use for further requests to the api endpoint. For the api endpoint I need to also add a header with the value set to the session token from the auth endpoint.

Endpoints setup in main.js

Auth endpoint:

config.registerEndpoint('auth',dfconfig.loginurl(),{headers: { "X-DreamFactory-Application-Name":app_name}});

Api endpoint:

config.registerEndpoint('api',dfconfig.database(),{headers: { "X-DreamFactory-Session-Token": token}});

I do not know the value of token until the auth endpoints returns the session value.

Can I just leave out the X-DreamFactory-Session-Token and then add it to auth endpoint when I actually use it? Or can I leave the header in as just set the value of token before I use the api endpoint?

RWOverdijk commented 7 years ago

@delebash Perhaps not useful for you, but aurelia-authentication does this stuff already (sending the access token). Might be worth checking out?

As to your question, you can at any point configure an endpoint. So you can decide to configure the defaults after fetching the token.

delebash commented 7 years ago

aurelia-authentication works great, thanks.

cindoum commented 7 years ago

If i dont want to use aurelia-auth (no need as already using a third party for auth), is there a way to change the value of the header? Or is it possible to pass a function to the header object so its called each time a request is triggered (like fetch client does...) =>

.withDefaults({
headers: {
    'Authorization': () => 'Bearer ' + localStorage.getItem('idToken')
    }
});

Thank you

RWOverdijk commented 7 years ago

@cindoum Yes. Check the docs or the authentication library.

Also, "third party" means it supplies a plugin for navigation, state etc? If so may I ask what you're using? :)