Skip to content

Instantly share code, notes, and snippets.

@philly-vanilly
Last active May 30, 2019 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philly-vanilly/3acb6167c864d7c64c965def7deb2ad1 to your computer and use it in GitHub Desktop.
Save philly-vanilly/3acb6167c864d7c64c965def7deb2ad1 to your computer and use it in GitHub Desktop.
@Injectable({
providedIn: 'root'
})
export class InitialAuthService {
private jwtHelper: JwtHelperService = new JwtHelperService();
private _decodedAccessToken: any;
private _decodedIDToken: any;
get decodedAccessToken() { return this._decodedAccessToken; }
get decodedIDToken() { return this._decodedIDToken; }
constructor(
private oauthService: OAuthService,
private authConfig: AuthConfig,
private injector: Injector
) {}
async initAuth(): Promise<any> {
return new Promise((resolveFn, rejectFn) => {
// setup oauthService
this.oauthService.configure(this.authConfig);
this.oauthService.setStorage(localStorage);
this.oauthService.tokenValidationHandler = new JwksValidationHandler();
// subscribe to token events
this.oauthService.events
.pipe(filter((e: any) => e.type === 'token_received'))
.subscribe(() => this.handleNewToken());
// continue initializing app (provoking a token_received event) or redirect to login-page
this.oauthService.loadDiscoveryDocumentAndTryLogin().then(isLoggedIn => {
if (isLoggedIn) {
this.oauthService.setupAutomaticSilentRefresh();
resolveFn();
// if you don't use clearHashAfterLogin from angular-oauth2-oidc you can remove the #hash or route to some other URL manually:
// const lazyPath = this.injector.get(LAZY_PATH) as string;
// this.injector.get(Router).navigateByUrl(lazyPath + '/a') // remove login hash fragments from url
// .then(() => resolveFn()); // callback only once login state is resolved
} else {
this.oauthService.initImplicitFlow();
rejectFn();
}
});
});
}
private handleNewToken() {
this._decodedAccessToken = this.jwtHelper.decodeToken(this.oauthService.getAccessToken());
this._decodedIDToken = this.jwtHelper.decodeToken(this.oauthService.getIdToken());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment