diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index a0f9d101..196756bf 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -20,7 +20,7 @@ const routes: Routes = [ { path: 'vault', component: VaultComponent, - canActivate: [AuthGuardService] + canActivate: [AuthGuardService], }, { path: 'hint', component: HintComponent }, ]; diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 2860f323..95a1cc5f 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -4,7 +4,27 @@ import { } from 'angular2-toaster'; import { Angulartics2GoogleAnalytics } from 'angulartics2/ga'; -import { Component } from '@angular/core'; +import { + Component, + OnInit, +} from '@angular/core'; +import { Router } from '@angular/router'; + +import { ToasterService } from 'angular2-toaster'; +import { Angulartics2 } from 'angulartics2'; + +import { BroadcasterService } from './services/broadcaster.service'; + +import { AuthService } from 'jslib/abstractions/auth.service'; +import { CipherService } from 'jslib/abstractions/cipher.service'; +import { CryptoService } from 'jslib/abstractions/crypto.service'; +import { FolderService } from 'jslib/abstractions/folder.service'; +import { I18nService } from 'jslib/abstractions/i18n.service'; +import { PasswordGenerationService } from 'jslib/abstractions/passwordGeneration.service'; +import { SettingsService } from 'jslib/abstractions/settings.service'; +import { SyncService } from 'jslib/abstractions/sync.service'; +import { TokenService } from 'jslib/abstractions/token.service'; +import { UserService } from 'jslib/abstractions/user.service'; @Component({ selector: 'app-root', @@ -13,7 +33,7 @@ import { Component } from '@angular/core'; `, }) -export class AppComponent { +export class AppComponent implements OnInit { toasterConfig: ToasterConfig = new ToasterConfig({ showCloseButton: true, mouseoverTimerStop: true, @@ -21,7 +41,59 @@ export class AppComponent { limit: 5, }); - constructor(angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics) { - // ctor + constructor(private angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics, + private broadcasterService: BroadcasterService, private userService: UserService, + private tokenService: TokenService, private folderService: FolderService, private cryptoService: CryptoService, + private settingsService: SettingsService, private syncService: SyncService, + private passwordGenerationService: PasswordGenerationService, private cipherService: CipherService, + private authService: AuthService, private router: Router, private analytics: Angulartics2, + private toasterService: ToasterService, private i18nService: I18nService) { } + + ngOnInit() { + this.broadcasterService.subscribe(async (message: any) => { + switch (message.command) { + case 'loggedIn': + break; + case 'logout': + const userId = await this.userService.getUserId(); + + await Promise.all([ + this.syncService.setLastSync(new Date(0)), + this.tokenService.clearToken(), + this.cryptoService.clearKeys(), + this.userService.clear(), + this.settingsService.clear(userId), + this.cipherService.clear(userId), + this.folderService.clear(userId), + this.passwordGenerationService.clear(), + ]); + + this.doneLoggingOut(message.expired); + break; + case 'doneLoggingOut': + this.doneLoggingOut(message.expired); + break; + case 'locked': + break; + case 'unlocked': + break; + case 'syncStarted': + break; + case 'syncCompleted': + break; + default: + } + }); + } + + private doneLoggingOut(expired: boolean) { + this.authService.logOut(() => { + this.analytics.eventTrack.next({ action: 'Logged Out' }); + if (expired) { + this.toasterService.popAsync('warning', this.i18nService.t('loggedOut'), + this.i18nService.t('loginExpired')); + } + this.router.navigate(['login']); + }); } } diff --git a/src/app/services/auth-guard.service.ts b/src/app/services/auth-guard.service.ts index 35e884af..bb6cdd23 100644 --- a/src/app/services/auth-guard.service.ts +++ b/src/app/services/auth-guard.service.ts @@ -5,19 +5,23 @@ import { } from '@angular/router'; import { CryptoService } from 'jslib/abstractions/crypto.service'; +import { MessagingService } from 'jslib/abstractions/messaging.service'; import { UserService } from 'jslib/abstractions/user.service'; @Injectable() export class AuthGuardService implements CanActivate { - constructor(private cryptoService: CryptoService, private userService: UserService, private router: Router) { } + constructor(private cryptoService: CryptoService, private userService: UserService, private router: Router, + private messagingService: MessagingService) { } - canActivate(): boolean { - if (!this.userService.isAuthenticated()) { - this.router.navigate(['vault']); + async canActivate() { + const isAuthed = await this.userService.isAuthenticated(); + if (!isAuthed) { + this.messagingService.send('logout'); return false; } - if (this.cryptoService.getKey() == null) { + const key = await this.cryptoService.getKey(); + if (key == null) { this.router.navigate(['lock']); return false; } diff --git a/src/app/services/broadcaster.service.ts b/src/app/services/broadcaster.service.ts new file mode 100644 index 00000000..659a9554 --- /dev/null +++ b/src/app/services/broadcaster.service.ts @@ -0,0 +1,3 @@ +import { EventEmitter } from '@angular/core'; + +export class BroadcasterService extends EventEmitter { } diff --git a/src/app/services/services.module.ts b/src/app/services/services.module.ts index 996113d0..164978d5 100644 --- a/src/app/services/services.module.ts +++ b/src/app/services/services.module.ts @@ -14,6 +14,7 @@ import { DesktopStorageService } from '../../services/desktopStorage.service'; import { I18nService } from '../../services/i18n.service'; import { AuthGuardService } from './auth-guard.service'; +import { BroadcasterService } from './broadcaster.service'; import { ValidationService } from './validation.service'; import { Analytics } from 'jslib/misc/analytics'; @@ -67,7 +68,8 @@ webFrame.registerURLSchemeAsPrivileged('file'); const i18nService = new I18nService(window.navigator.language, './locales'); const utilsService = new UtilsService(); const platformUtilsService = new DesktopPlatformUtilsService(i18nService); -const messagingService = new DesktopMessagingService(); +const broadcasterService = new BroadcasterService(); +const messagingService = new DesktopMessagingService(broadcasterService); const storageService: StorageServiceAbstraction = new DesktopStorageService(); const secureStorageService: StorageServiceAbstraction = new DesktopSecureStorageService(); const constantsService = new ConstantsService({}, 0); @@ -136,6 +138,9 @@ function initFactory(i18n: I18nService, platformUtils: DesktopPlatformUtilsServi { provide: ApiServiceAbstraction, useValue: apiService }, { provide: SyncServiceAbstraction, useValue: syncService }, { provide: UserServiceAbstraction, useValue: userService }, + { provide: MessagingServiceAbstraction, useValue: messagingService }, + { provide: BroadcasterService, useValue: broadcasterService }, + { provide: SettingsServiceAbstraction, useValue: settingsService }, { provide: APP_INITIALIZER, useFactory: initFactory, diff --git a/src/locales/en/messages.json b/src/locales/en/messages.json index 6df75a54..69517af7 100644 --- a/src/locales/en/messages.json +++ b/src/locales/en/messages.json @@ -594,5 +594,11 @@ }, "featureUnavailable": { "message": "Feature Unavailable" + }, + "loggedOut": { + "message": "Logged out" + }, + "loginExpired": { + "message": "Your login session has expired." } } diff --git a/src/main.ts b/src/main.ts index a56af68f..f14ad956 100644 --- a/src/main.ts +++ b/src/main.ts @@ -30,6 +30,20 @@ import { I18nService } from './services/i18n.service'; const i18nService = new I18nService('en', './locales/'); i18nService.init().then(() => { }); +ipcMain.on('messagingService', async (event: any, message: any) => { + switch (message.command) { + case 'loggedIn': + break; + case 'logout': + break; + case 'syncCompleted': + console.log('sync completed!!'); + break; + default: + break; + } +}); + let win: BrowserWindow; const args = process.argv.slice(1); const watch = args.some((val) => val === '--watch'); diff --git a/src/services/desktopMessaging.service.ts b/src/services/desktopMessaging.service.ts index ed39cad1..ccb242ee 100644 --- a/src/services/desktopMessaging.service.ts +++ b/src/services/desktopMessaging.service.ts @@ -1,11 +1,15 @@ -import { - MessagingService, - PlatformUtilsService, -} from 'jslib/abstractions'; +import { ipcRenderer } from 'electron'; + +import { MessagingService } from 'jslib/abstractions'; + +import { BroadcasterService } from '../app/services/broadcaster.service'; export class DesktopMessagingService implements MessagingService { + constructor(private broadcasterService: BroadcasterService) { } + send(subscriber: string, arg: any = {}) { const message = Object.assign({}, { command: subscriber }, arg); - // TODO + ipcRenderer.send('messagingService', message); + this.broadcasterService.emit(message); } }