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

Override headers not working #11

Open
masiorama opened this issue Dec 15, 2017 · 6 comments
Open

Override headers not working #11

masiorama opened this issue Dec 15, 2017 · 6 comments

Comments

@masiorama
Copy link

masiorama commented Dec 15, 2017

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

Could it be related to angular/angular#20554?

@0xMatt
Copy link
Owner

0xMatt commented Dec 15, 2017

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

@masiorama
Copy link
Author

masiorama commented Dec 15, 2017

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

0xMatt commented Mar 11, 2018

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

Thanks, I'm gonna look forward to it.

@masiorama
Copy link
Author

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

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

2 participants