SpoonX/aurelia-api

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

Closed this issue · 4 comments

This question may be related to #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?

@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.

aurelia-authentication works great, thanks.

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

@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? :)