I'm implementing OAuth2 in an existing Angular 6 application using okta-angular
package. I'm running into an issue where the word 'Bearer' is getting removed from or not reaching the Authorization request header in any HTTP calls. This results in 401 unauthorized. Any insight into why this happening and a direction for how to correct it?
The base.service.ts getHeaders()
method handles generating headers for all HTTP calls in the app and includes the word 'Bearer' before the auth token.
protected async getHeaders(): Promise<boolean> {
if (await this.oktaService.isAuthenticated()) {
this.headers = new HttpHeaders({ Authorization: `Bearer ${this.oktaService.getAccessToken()}`,
'Content-Type': 'application/json', 'Accept': 'application/json', 'Cache-Control': 'no-cache' });
return true;
}
return false;
}
base.service.ts get()
method using this.headers
public async get(methodRoute: string, param?: string): Promise<Observable<ResponseDTO<T>>> {
if (await this.getHeaders()) {
let fullUrl = `${this.entityUrl + methodRoute}`;
if (param !== undefined && param !== null && param.length > 0) {
fullUrl += '/' + param;
}
return this.http.get<ResponseDTO<T>>(fullUrl, { headers: this.headers });
}
return null;
}
nav-menu.component.ts using baseService's get()
method
async load() {
if (this.isAuthenticated) {
(await this.baseService.get('url/goes/here'))
.subscribe((response: ResponseDTO<boolean>) => {
this.isSup.next(response.responseObject);
});
(await this.baseService.get('url/goes/here'))
.subscribe((response: ResponseDTO<boolean>) => {
this.isKSup.next(response.responseObject);
})
}
}
Running the code I can observe this.headers
and see "Bearer {token here}" in full, explicitly with the word Bearer intact. In the F12 Developer Tool's console the HTTP request returns a 401 Unauthorized error. Looking at the request headers via Dev Tool's (both Chrome and Edge) I see the word 'Bearer' is missing in the Authorization header, but the token is present.
Screenshot of Authorization header
Relevant items from package.json
"@angular/cdk": "^6.4.7",
"@angular/common": "^6.1.10",
"@angular/compiler": "^6.1.10",
"@angular/core": "^6.1.10",
"@angular/forms": "^6.1.10",
"@angular/http": "^6.1.10",
"@okta/okta-angular": "^3.1.0",
"@okta/okta-auth-js": "^5.0.2",
"node-sass": "^4.14.1",
"rxjs": "^6.6.3",
I have tried downgrading to 6.1.7, which matches a different project that correctly passes 'Bearer' along to the request headers. No dice. I've tried restructuring the load()
method to get headers first and use them inline, similar to the get() method, but that didn't work either.