import { OAuthService } from 'angular-oauth2-oidc' ; import { Store } from '@ngxs/store' ; import { LoginSuccess } from './auth.actions' ; import { AuthorizationErrorResponse } from './oauth.errors' ; import { authConfigImplicit , authConfigCodeFlow } from './oauth.config' ; export function initializeAuth ( oauthService , store ) { oauthService . configure ( authConfigImplicit ) ; oauthService . setStorage ( sessionStorage ) ; return async ( ) => { await oauthService . loadDiscoveryDocumentAndTryLogin ( { onLoginError : ( err ) => { console . log ( `Error Code: ${ err . error }, Error Description: ${ err . error_description }` ) ; } , } ) ; if ( oauthService . hasValidAccessToken ( ) ) { console . log ( 'yes ValidAccessToken' ) ; const profile = oauthService . getIdentityClaims ( ) ; store . dispatch ( new LoginSuccess ( profile ) ) ; } else { console . log ( 'no ValidAccessToken' ) ; } return true ; } ; }