Untitled

mail@pastecode.io avatar
unknown
typescript
2 years ago
1.1 kB
8
Indexable
import { Injectable } from '@angular/core';
import { OAuthService } from 'angular-oauth2-oidc';
import { authConfig } from '../configs/auth-config';
import { UserInfoService } from './user-info.service';
import { ExtendedUserInfo } from '../models/extended-user-info';

@Injectable()
export class AuthService {

  constructor(
    private oAuthService: OAuthService,
    private userInfoService: UserInfoService
  ) { }

  public configureCodeFlow(): void {
    this.oAuthService.configure(authConfig);
    this.oAuthService.setupAutomaticSilentRefresh();

    this.oAuthService.loadDiscoveryDocument().then(() => {
      this.oAuthService.tryLogin().then(() => {
        if (!this.oAuthService.hasValidIdToken() || !this.oAuthService.hasValidAccessToken()) {
          this.oAuthService.initCodeFlow(window.location.href);
        } 
        if (this.oAuthService.hasValidAccessToken()) {
          this.oAuthService.loadUserProfile().then((response: any) => {
            this.userInfoService.update(new ExtendedUserInfo().deserialize(response.info));
          });
        }
      });
    });

  }

}