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

Add header on refresh #13

Open
mcalthrop opened this issue Oct 11, 2017 · 3 comments
Open

Add header on refresh #13

mcalthrop opened this issue Oct 11, 2017 · 3 comments

Comments

@mcalthrop
Copy link

Hi

I'd like to be able to add a header on every refresh. The value of the header will be calculated (and unique) every time the refresh endpoint is hit.

I can't see how to do this currently... can anyone help?

many thanks

Matt

@mcalthrop
Copy link
Author

I found a workaround: I have subclassed the JwtHttp class, and overridden the request() method – something like this:

class MyHttp extends JwtHttp {
    request(urlOrRequest: string | Request, options: RequestOptionsArgs = {}) {
        // Here is where I modify the headers, before calling `super()`
        return super.request(urlOrRequest, options);
    }
}

It's not an ideal solution, because it affects all HTTP requests. In my use case, that is what I need, but it would be useful to be able to configure the refresh token HTTP request.

Any suggestions @leonardobazico?

@mcalthrop
Copy link
Author

Scratch that. I've just discovered that the above method does not work: when the HTTP request is made to refresh the token, the request() method above is not invoked.

So I'm currently at a dead end... @leonardobazico if you've got a moment to have a look, it would be much appreciated. :-)

@mcalthrop
Copy link
Author

mcalthrop commented Oct 16, 2017

Ok, so here's one solution to the original problem I described: override the _refreshTheToken() method in the class that I extend from JwtHttp – something like this:

export class MyHttp extends JwtHttp {
    constructor(
        refreshConfigService: JwtConfigService,
        http: Http,
        defOpts?: RequestOptions,
    ) {
        super(refreshConfigService, http, defOpts);
    }

    protected _refreshTheToken() {
        // keep a copy of current headers by cloning them
        const savedGlobalHeaders: Array<Object> = this._config.globalHeaders.slice(0);
        // and then add the headers I want here
        this._config.globalHeaders.push('header-name', 'header-value');

        return super._refreshTheToken().do(() => {
            // restore the headers we saved
            this._config.globalHeaders = savedGlobalHeaders;
        });
    }
}

It's a bit messy, but it does the job. @leonardobazico, what do you think?

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

1 participant