Aurelia server side views: Adding auth header to h

2019-09-11 16:31发布

问题:

I'm using the sample Aurelia skeleton project (skeleton-esnext-aspnetcore), with a C#/MVC backend from: https://github.com/aurelia/skeleton-navigation/tree/master/skeleton-esnext-aspnetcore

To support server side views, I updated the view locator prototype (ViewLocator.prototype.convertOriginToViewUrl) in main.js to request the view/viewmodel files from the server. Details here: http://aurelia.io/hub.html#/doc/article/aurelia/framework/latest/app-configuration-and-startup/9

This works fine, and a sample view/viewmodel are successfully downloaded/loaded from the server by systemJS.

I would like to add a custom auth header to the http view/viewmodel request, to identity the user on the back-end. How would I configure this in Aurelia?

I have an existing service using the Aurelia fetch client w/interceptors, that is sending the auth headers to the API. I tried initializing the fetch client config earlier in the app/main.js, but it didn't appear to resolve the issue. Thanks!

回答1:

I found a reference to the SystemJS configuration API: https://github.com/systemjs/systemjs/blob/master/docs/config-api.md

As a quick test, I added the following code to the existing script block in index.html:

 <script>
    System.config({
        meta: {
            '*': {
                'authorization': 'bearer 123'
            }  
        }
    });
    System.import('aurelia-bootstrapper');
</script>

After verifying, the header was added, I was able to confirm that the code can be moved into a normal class, like main.js:

  System.config({
      meta: {
          '*': {
              'authorization': 'bearer 123456'
          }  
      }
  });


标签: aurelia