Created
January 13, 2020 14:57
-
-
Save Ch3shireDev/1c117f883a8b5ce41f5b87a4875eeae8 to your computer and use it in GitHub Desktop.
Angular OAuth2 connection
This file contains hidden or 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
import { Injectable } from '@angular/core'; | |
import { AuthConfig, JwksValidationHandler, OAuthService, LoginOptions } from 'angular-oauth2-oidc'; | |
import { JwtHelperService } from '@auth0/angular-jwt'; | |
import { filter } from 'rxjs/operators'; | |
import { HttpHeaders } from '@angular/common/http'; | |
@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 | |
) { } | |
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(); | |
// } else { | |
// // this.oauthService.initImplicitFlow(); | |
// // this.oauthService.initImplicitFlowInternal(); | |
// // rejectFn(); | |
// } | |
// }); | |
// this.oauthService. | |
// this.oauthService.tryLogin().then(isLoggedIn => { | |
// if (isLoggedIn) { | |
// this.oauthService.setupAutomaticSilentRefresh(); | |
// resolveFn(); | |
// } else { | |
// // this.oauthService.initImplicitFlow(); | |
// // this.oauthService.initImplicitFlowInternal(); | |
// rejectFn(); | |
// } | |
// }); | |
// .then(() => { | |
// console.debug('successfully logged in'); | |
// // this.loginFailed = false; | |
// this.oauthService.setupAutomaticSilentRefresh(); | |
// resolveFn(); | |
// }) | |
// .catch(err => { | |
// console.error('error logging in', err); | |
// // this.loginFailed = true; | |
// }); | |
// this.oauthService.fetchTokenUsingPasswordFlow('[email protected]', 'haslo'); | |
// console.log("kopytko") | |
}); | |
} | |
private handleNewToken() { | |
this.DecodedAccessToken = this.jwtHelper.decodeToken(this.oauthService.getAccessToken()); | |
this.DecodedIDToken = this.jwtHelper.decodeToken(this.oauthService.getIdToken()); | |
} | |
public login() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment