Resolve "Auth providers should be ready on components init"

This commit is contained in:
Jovan Krunić
2022-05-13 09:05:33 +00:00
parent 28caaf1d21
commit b7ae2cf019
11 changed files with 77 additions and 96 deletions

View File

@@ -31,6 +31,10 @@ import {PAIAAuthorizationResponse} from './paia-authorization-response';
import {PAIAAuthorizationNotifier} from './paia-authorization-notifier';
import {PAIATokenResponse} from './paia-token-response';
import {IPAIAAuthAction, PAIAAuthActionBuilder} from './paia-auth-action';
import {SCAuthorizationProvider} from '@openstapps/core';
import {ConfigProvider} from '../../config/config.provider';
import {getClientConfig, getEndpointsConfig} from '../auth.provider.methods';
import {Injectable} from '@angular/core';
const TOKEN_KEY = 'auth_paia_token';
const AUTH_EXPIRY_BUFFER = 10 * 60 * -1; // 10 mins in seconds
@@ -44,7 +48,10 @@ export interface IAuthService {
getValidToken(buffer?: number): Promise<PAIATokenResponse>;
}
export class PAIAAuthService implements IAuthService {
@Injectable({
providedIn: 'root',
})
export class PAIAAuthService {
private _authConfig?: IAuthConfig;
private _authSubject: AuthSubject = new AuthSubject();
@@ -78,14 +85,14 @@ export class PAIAAuthService implements IAuthService {
protected browser: Browser = new DefaultBrowser(),
protected storage: StorageBackend = new LocalStorageBackend(),
protected requestor: Requestor = new JQueryRequestor(),
utils = new BasicQueryStringUtils(),
private readonly configProvider: ConfigProvider,
) {
this.tokenHandler = new PAIATokenRequestHandler(requestor);
this.userInfoHandler = new IonicUserInfoHandler(requestor);
this.requestHandler = new PAIAAuthorizationRequestHandler(
browser,
storage,
utils,
new BasicQueryStringUtils(),
crypto,
);
this.endSessionHandler = new IonicEndSessionHandler(browser);
@@ -130,8 +137,19 @@ export class PAIAAuthService implements IAuthService {
}
public async init() {
this.setupConfiguration();
this.setupAuthorizationNotifier();
this.loadTokenFromStorage();
await this.loadTokenFromStorage();
}
setupConfiguration() {
const authConfig = this.configProvider.getAnyValue('auth') as {
paia: SCAuthorizationProvider;
};
this.authConfig = getClientConfig('paia', authConfig);
this.localConfiguration = new AuthorizationServiceConfiguration(
getEndpointsConfig('paia', authConfig),
);
}
protected notifyActionListers(action: IPAIAAuthAction) {