From e4b0012433ced57932d2574444e4d2b523c287fa Mon Sep 17 00:00:00 2001 From: Georgi Prodanov Date: Mon, 22 May 2017 17:38:36 +0300 Subject: [PATCH] Fix issue with users signing up and not subscribing to push notifications. --- app/app.component.ts | 9 ++++++++- app/services/push-notifications.service.ts | 2 +- app/users/login/login.component.ts | 22 ++++++++++------------ 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/app/app.component.ts b/app/app.component.ts index 298e316..2d64081 100644 --- a/app/app.component.ts +++ b/app/app.component.ts @@ -3,7 +3,7 @@ import { NavigationEnd, NavigationStart } from '@angular/router'; import { RouterExtensions } from 'nativescript-angular/router'; import { Page } from 'ui/page'; import { utilities } from './shared'; -import { FilesService } from './services'; +import { FilesService, UsersService, PushNotificationsService } from './services'; @Component({ moduleId: module.id, @@ -16,6 +16,8 @@ export class AppComponent implements OnInit { constructor( private _page: Page, private _filesService: FilesService, + private _usersService: UsersService, + private _push: PushNotificationsService, private _routerExtensions: RouterExtensions ) {} @@ -30,5 +32,10 @@ export class AppComponent implements OnInit { }); this._filesService.emptyAppTempFolder(); // .catch(err => console.log('err while emptying: ' + JSON.stringify(err))); + this._usersService.currentUser().then(user => { + if (user) { + this._push.subscribe().catch(err => err); // ignore errors + } + }); } } diff --git a/app/services/push-notifications.service.ts b/app/services/push-notifications.service.ts index 7926959..92aa97b 100644 --- a/app/services/push-notifications.service.ts +++ b/app/services/push-notifications.service.ts @@ -24,7 +24,7 @@ export class PushNotificationsService { // this._alertsService.showSuccess('got push' + JSON.stringify(args)); } - subscribe(userId: string) { + subscribe() { let pushRegSettings = { iOS: { badge: true, sound: true, alert: true }, android: { senderID: constants.androidProjNumber }, diff --git a/app/users/login/login.component.ts b/app/users/login/login.component.ts index 8a7d43d..2036cca 100644 --- a/app/users/login/login.component.ts +++ b/app/users/login/login.component.ts @@ -46,11 +46,7 @@ export class LoginComponent implements OnInit { } login() { - this._usersService.login(this.user.Username, this.user.Password) - .then((loginResult) => { - this._push.subscribe(loginResult.result.principal_id).catch(err => err); - this._goToEvents(); - }) + this._logInUser(this.user.Username, this.user.Password) .catch((e) => { this._alertsService.showError(e && e.message); }); @@ -65,17 +61,11 @@ export class LoginComponent implements OnInit { if (errMsg) { return this._alertsService.showError(errMsg); } - let registeredUserId: string; this._usersService.register(this.user.Username, this.user.Password, this.user.DisplayName) .then((res) => { this._alertsService.showSuccess('Welcome to TeamUP!'); - registeredUserId = res.result.Id; - return this._usersService.login(this.user.Username, this.user.Password); - }) - .then(() => { - this._push.subscribe(registeredUserId).catch(err => err); - this._goToEvents(); + return this._logInUser(this.user.Username, this.user.Password); }) .catch((err) => { if (err) { @@ -106,4 +96,12 @@ export class LoginComponent implements OnInit { let transition = utilities.getPageTransition(); this._routerExtensions.navigate(['events'], { clearHistory: true, transition }); } + + private _logInUser(username: string, password: string) { + return this._usersService.login(this.user.Username, this.user.Password) + .then(() => { + this._push.subscribe().catch(e => e); // ignore errors + this._goToEvents(); + }); + } }