messaging service implementation

This commit is contained in:
Kyle Spearrin 2018-02-08 12:24:17 -05:00
parent 6452cfaf7f
commit 825cc5fd37
8 changed files with 124 additions and 16 deletions

View File

@ -20,7 +20,7 @@ const routes: Routes = [
{
path: 'vault',
component: VaultComponent,
canActivate: [AuthGuardService]
canActivate: [AuthGuardService],
},
{ path: 'hint', component: HintComponent },
];

View File

@ -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';
<toaster-container [toasterconfig]="toasterConfig"></toaster-container>
<router-outlet></router-outlet>`,
})
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']);
});
}
}

View File

@ -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;
}

View File

@ -0,0 +1,3 @@
import { EventEmitter } from '@angular/core';
export class BroadcasterService extends EventEmitter<any> { }

View File

@ -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,

View File

@ -594,5 +594,11 @@
},
"featureUnavailable": {
"message": "Feature Unavailable"
},
"loggedOut": {
"message": "Logged out"
},
"loginExpired": {
"message": "Your login session has expired."
}
}

View File

@ -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');

View File

@ -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);
}
}