eddie
August 1, 2019, 6:34pm
1
It seems that all examples are using @angular /common/http is there an example on how to use Okta SDK with older @angular /http?
The issue I am having is that I don’t want to change my methods to async/await and hence can’t execute await this.oktaAuth.getAccessToken();
I understand it is possible to extend http and implement “Interceptor” functionality but is there an example of how to do that with Okta SDK?
thanks
Try:
this.oktaAuth.getAccessToken().then(() => {
// do something
});
auth-interceptor.ts
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
import { AuthService } from 'auth';
import { LoggerService } from 'utils';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private authService: AuthService,
private logger: LoggerService) {
this.logger.info('AuthInterceptor: constructor()');
}
intercept(req: HttpRequest<any>, next: HttpHandler) {
const accessToken = this.authService.getAccessToken();
if (accessToken) {
this.logger.info('AuthInterceptor: intercept() Bearer ' + accessToken);
const authReq = req.clone({ setHeaders: { Authorization: 'Bearer ' + accessToken } });
return next.handle(authReq);
}
return next.handle(req);
}
}
The Angular wrapper for @okta/okta-auth-js
doesn’t support the Authorisation Code flow with PKCE so you will need to write your own wrapper for @okta/okta-auth-js
.
For example: Angular Wrapper for Okta Auth JavaScript SDK
Auth Resources