sureshchahal / angular2-adal

Angular 2 wrapper for adal.js
MIT License
41 stars 50 forks source link

Update for Angular 5? #76

Open jo-me opened 6 years ago

jo-me commented 6 years ago

Hi,

just noticed that things break when upgrading to Angular 5. Seems like mismatches in the dependencies.

Also, Angular 5 promotes using a new http client (from @angular/common/http).

See here for details about Angular 5: https://blog.angular.io/version-5-0-0-of-angular-now-available-37e414935ced

Is there a new version of ng2-adal planned?

Thanks

dilotec-2015 commented 6 years ago

if you write your own AuthHttp Service ng2-adal works with angular 5 as expected.

dilotec-2015 commented 6 years ago

something like this:

type RequestOptionsArgs = {
    body?: any,
    headers?: HttpHeaders,
    observe?: any,
    params?: HttpParams,
    reportProgress?: boolean,
    responseType?: any,
    withCredentials?: boolean,
}

@Injectable()
export class AuthHttp {

    private authHeaders: HttpHeaders;
    private tokenResource: string;

    constructor(
        private http: HttpClient,
        private adalService: AdalService,
        private adalConfigService: AdalConfigService,
    ) {
        this.tokenResource = this.adalConfigService.adalConfig.clientId;
    }

    private _setAuthHeaders(access_token, token_type = 'Bearer') {
        access_token = access_token || this.adalService.getCachedToken(this.adalConfigService.adalConfig.clientId);
        this.authHeaders = new HttpHeaders();
        this.authHeaders = this.authHeaders.append('Authorization', token_type + ' ' + access_token);
        this.authHeaders = this.authHeaders.append('Content-Type', 'application/json');
    }

    public _setRequestOptions(options?: RequestOptionsArgs) {
        if (options && options.hasOwnProperty('headers')) {
            options.headers = options.headers.append('Authorization', this.authHeaders.getAll('Authorization'));
        } else {
            options = ({headers: this.authHeaders});
        }
        return options;
    }
    /**
   * Example of how you can make auth request using angulars http methods.
   * @param options if options are not supplied the default content type is application/json
   */
    get(url: string, options?: RequestOptionsArgs): Observable {
        return this.adalService
            .acquireToken(this.tokenResource)
            .flatMap((token) => {
                this._setAuthHeaders(token);
                return this.http.get(url, this._setRequestOptions(options));
            });
    }
}
jo-me commented 6 years ago

Thanks for the hint!

dilotec-2015 commented 6 years ago

you are welcome :)

rjwijnen commented 6 years ago

I tried to edit this library to support. Please check if helps you out: #77

hkusulja commented 6 years ago

Duplicate of #81 and #76

jo-me commented 6 years ago

Since it broke again after the update to Angular 5.1 or 5.2 I moved on to "adal-angular5"

Lecha1 commented 6 years ago

jo-me but adal-angular5 does not work!

saransh944 commented 5 years ago

@dilotec-2015 , I have created my own AuthHttp service just like shown above, but I'm getting this error saying "ERROR TypeError: Cannot read property 'getResourceForEndpoint' of undefined", where the undefined piece is the adal service instance. Any thoughts on this?

dilotec-2015 commented 5 years ago

Hi,

i’m switched to "adal-angular4": "^2.0.0", working without problems with angular 5 and 6

Von: saransh944 [mailto:notifications@github.com] Gesendet: Mittwoch, 8. August 2018 12:36 An: sureshchahal/angular2-adal angular2-adal@noreply.github.com Cc: Waldemar Scholtyssek W.Scholtyssek@dilotec.de; Mention mention@noreply.github.com Betreff: Re: [sureshchahal/angular2-adal] Update for Angular 5? (#76)

@dilotec-2015https://github.com/dilotec-2015 , I have created my own AuthHttp service just like shown above, but I'm getting this error saying "ERROR TypeError: Cannot read property 'getResourceForEndpoint' of undefined", where the undefined piece is the adal service instance. Any thoughts on this?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/sureshchahal/angular2-adal/issues/76#issuecomment-411362425, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AQN81OePK4bildETKvNyPMQWl2LdpCMpks5uOr7_gaJpZM4QWGtY.