diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 266125bbf..0b48170bc 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -31,7 +31,7 @@ import { PageTitleService, UploadService } from '@alfresco/adf-core'; -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, OnDestroy } from '@angular/core'; import { ActivatedRoute, NavigationEnd, Router } from '@angular/router'; import { Store } from '@ngrx/store'; import { AppExtensionService } from './extensions/extension.service'; @@ -40,24 +40,28 @@ import { SetCurrentUrlAction, SetInitialStateAction, CloseModalDialogsAction, - SetRepositoryStatusAction + SetRepositoryStatusAction, + SetUserProfileAction } from './store/actions'; import { AppStore, AppState, INITIAL_APP_STATE } from './store/states/app.state'; -import { filter } from 'rxjs/operators'; +import { filter, takeUntil } from 'rxjs/operators'; import { ContentApiService } from './services/content-api.service'; import { DiscoveryEntry } from 'alfresco-js-api'; import { AppService } from './services/app.service'; +import { Subject } from 'rxjs'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'] }) -export class AppComponent implements OnInit { +export class AppComponent implements OnInit, OnDestroy { + onDestroy$: Subject = new Subject(); + constructor( private route: ActivatedRoute, private router: Router, @@ -83,11 +87,6 @@ export class AppComponent implements OnInit { this.store.dispatch(new CloseModalDialogsAction()); this.router.navigate(['/login']); - - this.appService.waitForAuth().subscribe(() => { - this.loadRepositoryStatus(); - // todo: load external auth-enabled plugins here - }); } } }); @@ -119,12 +118,18 @@ export class AppComponent implements OnInit { this.onFileUploadedError(error) ); - this.appService.waitForAuth().subscribe(() => { + this.appService.ready$.pipe(takeUntil(this.onDestroy$)).subscribe(() => { this.loadRepositoryStatus(); + this.loadUserProfile(); // todo: load external auth-enabled plugins here }); } + ngOnDestroy() { + this.onDestroy$.next(true); + this.onDestroy$.complete(); + } + private loadRepositoryStatus() { this.contentApi .getRepositoryInformation() @@ -135,6 +140,12 @@ export class AppComponent implements OnInit { }); } + private loadUserProfile() { + this.contentApi.getPerson('-me-').subscribe(person => { + this.store.dispatch(new SetUserProfileAction(person.entry)); + }); + } + private loadAppSettings() { const baseShareUrl = this.config.get('baseShareUrl') || diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts index 0261f468c..c1a2e52b7 100644 --- a/src/app/app.routes.ts +++ b/src/app/app.routes.ts @@ -29,7 +29,6 @@ import { FilesComponent } from './components/files/files.component'; import { LibrariesComponent } from './components/libraries/libraries.component'; import { GenericErrorComponent } from './components/common/generic-error/generic-error.component'; import { SearchResultsComponent } from './components/search/search-results/search-results.component'; -import { ProfileResolver } from './services/profile.resolver'; import { LoginComponent } from './components/login/login.component'; import { AppAuthGuard } from './guards/auth.guard'; import { AppSharedRuleGuard } from './guards/shared.guard'; @@ -55,9 +54,6 @@ export const APP_ROUTES: Routes = [ { path: '', component: LayoutComponent, - resolve: { - profile: ProfileResolver - }, children: [ { path: '', diff --git a/src/app/extensions/extension.service.ts b/src/app/extensions/extension.service.ts index daa7865a8..5825662be 100644 --- a/src/app/extensions/extension.service.ts +++ b/src/app/extensions/extension.service.ts @@ -29,7 +29,6 @@ import { Route } from '@angular/router'; import { AppStore, RepositoryState } from '../store/states'; import { ruleContext } from '../store/selectors/app.selectors'; import { NodePermissionService } from '../services/node-permission.service'; -import { ProfileResolver } from '../services/profile.resolver'; import { SelectionState, NavigationState, @@ -224,7 +223,6 @@ export class AppExtensionService implements RuleContext { component: this.getComponentById(route.layout || this.defaults.layout), canActivateChild: guards, canActivate: guards, - resolve: { profile: ProfileResolver }, children: [ { path: '', diff --git a/src/app/services/app.service.ts b/src/app/services/app.service.ts index 0c86cb333..24b7f86e0 100644 --- a/src/app/services/app.service.ts +++ b/src/app/services/app.service.ts @@ -25,21 +25,21 @@ import { Injectable } from '@angular/core'; import { AuthenticationService } from '@alfresco/adf-core'; -import { Observable, of } from 'rxjs'; -import { take } from 'rxjs/operators'; +import { Observable, BehaviorSubject } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class AppService { - constructor(private auth: AuthenticationService) {} + private ready: BehaviorSubject; + ready$: Observable; - waitForAuth(): Observable { - const isLoggedIn = this.auth.isLoggedIn(); - if (isLoggedIn) { - return of(true); - } else { - return this.auth.onLogin.pipe(take(1)); - } + constructor(auth: AuthenticationService) { + this.ready = new BehaviorSubject(auth.isLoggedIn()); + this.ready$ = this.ready.asObservable(); + + auth.onLogin.subscribe(e => { + this.ready.next(true); + }); } } diff --git a/src/app/services/profile.resolver.spec.ts b/src/app/services/profile.resolver.spec.ts deleted file mode 100644 index 318525b93..000000000 --- a/src/app/services/profile.resolver.spec.ts +++ /dev/null @@ -1,32 +0,0 @@ -/*! - * @license - * Alfresco Example Content Application - * - * Copyright (C) 2005 - 2018 Alfresco Software Limited - * - * This file is part of the Alfresco Example Content Application. - * If the software was purchased under a paid Alfresco license, the terms of - * the paid license agreement will prevail. Otherwise, the software is - * provided under the following open source license terms: - * - * The Alfresco Example Content Application is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * The Alfresco Example Content Application is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Alfresco. If not, see . - */ - -import { ProfileResolver } from './profile.resolver'; - -describe('ProfileResolver', () => { - it('should be defined', () => { - expect(ProfileResolver).toBeDefined(); - }); -}); diff --git a/src/app/services/profile.resolver.ts b/src/app/services/profile.resolver.ts deleted file mode 100644 index 9f24222da..000000000 --- a/src/app/services/profile.resolver.ts +++ /dev/null @@ -1,63 +0,0 @@ -/*! - * @license - * Alfresco Example Content Application - * - * Copyright (C) 2005 - 2018 Alfresco Software Limited - * - * This file is part of the Alfresco Example Content Application. - * If the software was purchased under a paid Alfresco license, the terms of - * the paid license agreement will prevail. Otherwise, the software is - * provided under the following open source license terms: - * - * The Alfresco Example Content Application is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * The Alfresco Example Content Application is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Alfresco. If not, see . - */ - -import { Store } from '@ngrx/store'; -import { Injectable } from '@angular/core'; -import { Resolve, Router } from '@angular/router'; -import { Person } from 'alfresco-js-api'; -import { Observable } from 'rxjs'; -import { AppStore } from '../store/states/app.state'; -import { SetUserProfileAction } from '../store/actions'; -import { ContentApiService } from './content-api.service'; - -@Injectable({ - providedIn: 'root' -}) -export class ProfileResolver implements Resolve { - constructor( - private store: Store, - private contentApi: ContentApiService, - private router: Router - ) {} - - resolve(): Observable { - return new Observable(observer => { - this.contentApi.getPerson('-me-').subscribe( - person => { - this.store.dispatch(new SetUserProfileAction(person.entry)); - observer.next(person.entry); - observer.complete(); - }, - err => { - if (err && err.status === 401) { - observer.next(null); - observer.complete(); - this.router.navigate(['login']); - } - } - ); - }); - } -}