refactor: get appropriate provider via auth helper

This commit is contained in:
Jovan Krunić
2022-02-14 11:12:02 +01:00
parent a5e5a5b407
commit eaf9da3c84
21 changed files with 141 additions and 182 deletions

View File

@@ -46,11 +46,13 @@
</ion-row>
<ion-row>
<ion-col class="login">
<a *ngIf="!data.default.loggedIn; else loggedIn" (click)="signIn()">{{
'profile.buttons.default.log_in' | translate | titlecase
}}</a>
<a
*ngIf="!data.default.loggedIn; else loggedIn"
(click)="signIn('default')"
>{{ 'profile.buttons.default.log_in' | translate | titlecase }}</a
>
<ng-template #loggedIn
><a (click)="signOut()">{{
><a (click)="signOut('default')">{{
'profile.buttons.default.log_out' | translate | titlecase
}}</a></ng-template
>
@@ -60,11 +62,11 @@
<ion-col class="login">
<a
*ngIf="!data.paia.loggedIn; else paiaLoggedIn"
(click)="signInPAIA()"
(click)="signIn('paia')"
>{{ 'profile.buttons.paia.log_in' | translate | titlecase }}</a
>
<ng-template #paiaLoggedIn
><a (click)="signOutPAIA()">{{
><a (click)="signOut('paia')">{{
'profile.buttons.paia.log_out' | translate | titlecase
}}</a></ng-template
>

View File

@@ -22,17 +22,25 @@ import {ProfilePageComponent} from './profile-page.component';
import {TranslateModule} from '@ngx-translate/core';
import {ConfigProvider} from '../../config/config.provider';
import {sampleAuthConfiguration} from '../../../_helpers/data/sample-configuration';
import {StorageProvider} from '../../storage/storage.provider';
describe('ProfilePage', () => {
let component: ProfilePageComponent;
let fixture: ComponentFixture<ProfilePageComponent>;
let configProvider: ConfigProvider;
let storageProvider: jasmine.SpyObj<StorageProvider>;
beforeEach(async(() => {
configProvider = jasmine.createSpyObj('ConfigProvider', [
'init',
'getAnyValue',
]);
storageProvider = jasmine.createSpyObj('StorageProvider', [
'init',
'get',
'has',
'put',
]);
configProvider.getAnyValue = jasmine.createSpy().and.callFake(function () {
return sampleAuthConfiguration;
});
@@ -45,7 +53,10 @@ describe('ProfilePage', () => {
AuthModule,
TranslateModule.forRoot(),
],
providers: [{provide: ConfigProvider, useValue: configProvider}],
providers: [
{provide: ConfigProvider, useValue: configProvider},
{provide: StorageProvider, useValue: storageProvider},
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
}).compileComponents();
}));

View File

@@ -15,9 +15,7 @@
import {Component, OnDestroy, OnInit} from '@angular/core';
import {IonicUserInfoHandler} from 'ionic-appauth';
import {DefaultAuthService} from '../../auth/default-auth.service';
import {Requestor, TokenResponse} from '@openid/appauth';
import {PAIAAuthService} from '../../auth/paia/paia-auth.service';
import {Subscription} from 'rxjs';
import {AuthHelperService} from '../../auth/auth-helper.service';
import {
@@ -44,8 +42,6 @@ export class ProfilePageComponent implements OnInit, OnDestroy {
subscriptions: Subscription[] = [];
constructor(
private defaultAuth: DefaultAuthService,
private paiaAuth: PAIAAuthService,
private requestor: Requestor,
private authHelper: AuthHelperService,
private route: ActivatedRoute,
@@ -53,8 +49,9 @@ export class ProfilePageComponent implements OnInit, OnDestroy {
ngOnInit() {
this.subscriptions.push(
this.defaultAuth.token$.subscribe(_token => {
this.defaultAuth
this.authHelper.getProvider('default').token$.subscribe(_token => {
this.authHelper
.getProvider('default')
.getValidToken()
.then(token => {
this.data.default.loggedIn = true;
@@ -64,8 +61,9 @@ export class ProfilePageComponent implements OnInit, OnDestroy {
this.data.default.loggedIn = false;
});
}),
this.paiaAuth.token$.subscribe(_token => {
this.paiaAuth
this.authHelper.getProvider('paia').token$.subscribe(_token => {
this.authHelper
.getProvider('paia')
.getValidToken()
.then(_token => {
this.data.paia.loggedIn = true;
@@ -74,8 +72,8 @@ export class ProfilePageComponent implements OnInit, OnDestroy {
this.data.paia.loggedIn = false;
});
}),
this.route.queryParamMap.subscribe(queryParams => {
this.originPath = queryParams.get('origin_path');
this.route.queryParamMap.subscribe(queryParameters => {
this.originPath = queryParameters.get('origin_path');
}),
);
}
@@ -84,31 +82,25 @@ export class ProfilePageComponent implements OnInit, OnDestroy {
const userInfoHandler = new IonicUserInfoHandler(this.requestor);
userInfoHandler
.performUserInfoRequest(this.defaultAuth.localConfiguration, token)
.performUserInfoRequest(
this.authHelper.getProvider('default').localConfiguration,
token,
)
.then(userInfo => {
this.userInfo = this.authHelper.getUserFromUserInfo(userInfo);
});
}
async signIn() {
async signIn(providerType: SCAuthorizationProviderType) {
await this.handleOriginPath();
this.defaultAuth.signIn();
this.authHelper.getProvider(providerType).signIn();
}
async signInPAIA() {
await this.handleOriginPath();
this.paiaAuth.signIn();
}
async signOut() {
await this.defaultAuth.signOut();
async signOut(providerType: SCAuthorizationProviderType) {
await this.authHelper.getProvider(providerType).signOut();
this.userInfo = undefined;
}
async signOutPAIA() {
await this.paiaAuth.signOut();
}
ngOnDestroy(): void {
for (const subscription of this.subscriptions) {
subscription.unsubscribe();