Reputation: 586
I already created a wrapper service for @angular/Http to add the logic of Bearer authentication and a counter of Http Requests.
I want to use this custom service to replace the breeze's one. How to accomplish this?
This is a simplest version of my wrapper
@import {Injectable} from '@angular/core'
import {Http, Headers} from '@angular/Http'
import {AuthService} from './auth.service'
@Injectable()
export class AuthHttpService
{
constructor(private http: Http, private authService: AuthService){}
get = (url: string){
//some logic for bearer authenticated calls
let params = {
// request extra info
};
return this.http.get(url, params);
}
}
My breeze manager fabric service. You can see authentication bearer logic. I want to the bearer appending code and change the entire Breeze's Http service.
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable, Scheduler } from 'rxjs/Rx';
import { LocalStorage, SessionStorage } from 'ng2-webstorage';
import { config, NamingConvention, EntityManager, EntityQuery, DataService, MetadataStore } from 'breeze-client';
import { AuthenticationService } from '../authentication/authentication.service';
import { BreezeProvider } from './breeze.provider.service';
import { Constants } from '../constants.class';
@Injectable()
export class EntityManagerProvider {
constructor(private provider: BreezeProvider, authenticationService: AuthenticationService) {
NamingConvention.camelCase.setAsDefault();
let ajaxAdapter = <any>config.getAdapterInstance('ajax', 'angular');
let headers = ajaxAdapter.defaultSettings['headers'] || {};
headers['Authorization'] = 'Bearer ' + authenticationService._userData.accessToken;
ajaxAdapter.defaultSettings['headers'] = headers;
}
}
Upvotes: 0
Views: 318
Reputation: 2006
Related to Angular (2/4), I found most of what I needed to get this working by looking at how Leonardo was approaching it in his answer and this discussion by Ward Bell and team here:.
Basically, replacing the ajax adapter with your own authorized version that has the bearer token included.
Per Ward:
import { AjaxAngular2Adapter } from 'breeze-bridge-angular2'; ...
// assume you injected auth0Http instance when you get here ... config.registerAdapter('ajax', () => new AjaxAngular2Adapter(auth0Http)); config.initializeAdapterInstance('ajax', AjaxAngular2Adapter.adapterName, true); ...
In my specific case, this meant changing my entity manager provider and also injecting my authService:
if (this.authService.isAuthenticated()) {
config.registerAdapter('ajax', () => new AjaxAngular2Adapter(<any>this.authService.authHttp));
config.initializeAdapterInstance('ajax', AjaxAngular2Adapter.adapterName, true);
}
// Unrelated, but including for context....
let dsconfig: DataServiceOptions = {
serviceName: EntityManagerProvider.serviceName
};
let dataService = new DataService(dsconfig);
let masterManager = EntityManagerProvider._masterManager = new EntityManager({
dataService: dataService
});
I'm still trying to work the the case where the user isn't authenticated when the entity manager provider isn't created, but later logs in. There is some discussion about not throwing the jwt error in the link, but I haven't entirely figured that out yet.
Upvotes: 0
Reputation: 586
I have been looking for any example when found this snipplet at breeze Doc site:
angular.module('app').run(['$http', function($http) {
var ajax = breeze.config.initializeAdapterInstance('ajax', 'angular');
ajax.setHttp($http); // use the $http instance that Angular injected into your app.
}]);
This example is for angularjs but help me to understand the replacement for
let ajaxAdapter = <any>config.getAdapterInstance('ajax', 'angular');
changed to
let ajaxAdapter = <any>config.getAdapterInstance('ajax', 'angular');
ajaxAdapter.http = authService;
and remove hard coded bearer lines
//let headers = ajaxAdapter.defaultSettings['headers'] || {};
//headers['Authorization'] = 'Bearer ' + authenticationService._userData.accessToken;
//ajaxAdapter.defaultSettings['headers'] = headers;
Update
The entire logic is as:
// Provider replacement
let ajaxAdapter = <any>config.getAdapterInstance('ajax', 'angular');
ajaxAdapter.http = authService;
//Default authentication settings
let headers = ajaxAdapter.defaultSettings['headers'] || {};
headers['Authorization'] = 'Bearer ' + authenticationService._userData.accessToken;
ajaxAdapter.defaultSettings['headers'] = headers;
Upvotes: 2