Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for Angular 5? #76

Open
jo-me opened this issue Nov 8, 2017 · 10 comments
Open

Update for Angular 5? #76

jo-me opened this issue Nov 8, 2017 · 10 comments

Comments

@jo-me
Copy link

jo-me commented Nov 8, 2017

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
Copy link

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

@dilotec-2015
Copy link

dilotec-2015 commented Nov 10, 2017

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
Copy link
Author

jo-me commented Nov 13, 2017

Thanks for the hint!

@dilotec-2015
Copy link

you are welcome :)

@rjwijnen
Copy link

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

@hkusulja
Copy link

Duplicate of #81 and #76

@jo-me
Copy link
Author

jo-me commented Jan 22, 2018

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

@Lecha1
Copy link

Lecha1 commented Feb 7, 2018

jo-me but adal-angular5 does not work!

@saransh944
Copy link

@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
Copy link

dilotec-2015 commented Aug 8, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants