Last active
May 30, 2019 09:24
-
-
Save philly-vanilly/3acb6167c864d7c64c965def7deb2ad1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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