0xMatt / ngx-oauth-client

An Angular4+ compatible OAuth2.0 compliant HTTP wrapper.
https://0xMatt.github.io/ngx-oauth-client
MIT License
39 stars 9 forks source link

Override headers not working #11

Open masiorama opened 6 years ago

masiorama commented 6 years ago

Hello, I'm trying to add auth header to my request, but it seems it gets lost at this point (ngx-request.js):

NgxRequest.prototype.setHeaders = function (headers, override) {
        this.headers = new http_1.HttpHeaders(Object.assign(headers, override));
        return this;
};

headers param is correctly populated with Object {Authorization: "Bearer 2f8e3907-93c5-4cd1-a81b-554d4811a675"}, and override param is undefined. The problem is at the point when the script create the new http_1.HttpHeaders object.

The returned object has:

HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: }

Any hint? Thanks in advance!

masiorama commented 6 years ago

Could it be related to https://github.com/angular/angular/issues/20554?

0xMatt commented 6 years ago

It's possible but I wouldn't rule out the problem being within this library itself. Can you post your code?

masiorama commented 6 years ago

This is my custom api.service (grabbed mostly from your demo):

import { Configuration, DefaultHeaders, NgxOAuthClient, NgxRequest } from 'ngx-oauth-client';
import { environment } from '../../../environments/environment';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/observable/throw';
import { RequestOptions, Headers } from '@angular/http';
import { HttpHeaders } from '@angular/common/http';

@Configuration(environment.api)
@DefaultHeaders({
     'Content-Type': 'application/json',
     'Accept': 'application/json'
 })
export class ApiService extends NgxOAuthClient {

requestInterceptor(request) {
    const token = this.fetchToken('access_token');
    if (token) {
        return request.setHeaders({Authorization: 'Bearer ' + token});
    }
    return request;
}

errorInterceptor(request, error): Observable<any> {
    if (error.status === 401) {
        const refresh_token = this.fetchToken('refresh_token');
        if (!refresh_token) {
            return Observable.throw(error);
        }
        return this.getToken('refresh_token', { refresh_token }).switchMap(token => {
            localStorage.setItem('auth_token', JSON.stringify(token));
            return this.getClient().request(
                request.method,
                request.url,
                this.requestInterceptor(request.setHeaders({ Authorization: 'Bearer ' + token }))
            );
        });
    }
    return Observable.throw(error);
 }

}

The token constant is correctly populated with the right access_token, but the request returned has no header in it. Thanks!

0xMatt commented 6 years ago

@masiorama, very sorry for the long delay in getting back to you on this issue. I'm taking more time into keeping this library in a fully working and maintained state. Please reply here if you don't hear a response from me by Wednesday regarding a fix.

I believe I was able to put an auto-login via refresh_token grant type in a personal project that was a much simpler approach. I'll try to also update the readme to include that method as well.

Thanks again for your patience!

masiorama commented 6 years ago

Thanks, I'm gonna look forward to it.

masiorama commented 6 years ago

Hello there @0xMatt, did you have time to handle it? Thanks in advance.