From 6e8c55ab76f200455b20ae6b78ae075620caa4d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eudes=20In=C3=A1cio?= Date: Mon, 1 Feb 2021 13:31:33 +0100 Subject: [PATCH] Save device token on middleware --- src/app/models/token.model.ts | 6 +++ src/app/pages/login/login.page.ts | 43 +++++++++++++------ .../services/notifications.service.spec.ts | 16 +++++++ src/app/services/notifications.service.ts | 30 +++++++++++++ 4 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 src/app/models/token.model.ts create mode 100644 src/app/services/notifications.service.spec.ts create mode 100644 src/app/services/notifications.service.ts diff --git a/src/app/models/token.model.ts b/src/app/models/token.model.ts new file mode 100644 index 000000000..062fe5f67 --- /dev/null +++ b/src/app/models/token.model.ts @@ -0,0 +1,6 @@ +export class Token { + UserId: number; + TokenId: string; + Status: number; + Service: number +} \ No newline at end of file diff --git a/src/app/pages/login/login.page.ts b/src/app/pages/login/login.page.ts index ba89bafd2..ba108f741 100644 --- a/src/app/pages/login/login.page.ts +++ b/src/app/pages/login/login.page.ts @@ -8,6 +8,9 @@ import { AlertController } from '@ionic/angular'; import { StorageService } from 'src/app/services/storage.service'; import { AuthConnstants } from 'src/app/config/auth-constants'; import { PhotoService } from 'src/app/services/photo.service'; +import { NotificationsService } from 'src/app/services/notifications.service'; +import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; +import { Token } from '../../models/token.model'; import { Plugins, PushNotification, PushNotificationToken, PushNotificationActionPerformed } from '@capacitor/core'; @@ -28,6 +31,8 @@ export class LoginPage implements OnInit { userattempt: User; constructor( + private http: HttpClient, + private notificatinsservice: NotificationsService, private router: Router, private authService: AuthService, private storageService: StorageService, @@ -57,21 +62,21 @@ export class LoginPage implements OnInit { await alert.present(); } - loginRocketChat(user:any){ - + loginRocketChat(user: any) { + let postData = { "user": user.username, "password": user.password, } - this.authService.loginChat(postData).subscribe((res: any) =>{ + this.authService.loginChat(postData).subscribe((res: any) => { console.log(res.data); this.storageService.store(AuthConnstants.AUTH, res.data); console.log('Login to Rocket chat OK'); - },(error:any) =>{ + }, (error: any) => { console.log('Network error'); - this.presentAlert('Network error '+error); + this.presentAlert('Network error ' + error); }); - } + } storeUserIdANdToken() { @@ -85,14 +90,26 @@ export class LoginPage implements OnInit { console.log('FIREBASE TOKEN', token.value) this.storageService.store(this.username, token.value); this.storageService.get(this.username).then(value => { - console.log('STORAGE TOKEN',value ) - this.storageService.get(AuthConnstants.USER).then(res=>{ - console.log('USERID',res); - + console.log('STORAGE TOKEN', value) + this.storageService.get(AuthConnstants.USER).then(res => { + console.log('USERID', res); + const headers = { 'Authorization': 'Basic cGF1bG8ucGludG9AZ2FiaW5ldGVkaWdpdGFsLmxvY2FsOnRhYnRlc3RlQDAwNg==' }; + const body = { UserId: 261, + TokenId: "cSLzVzMzRCKZvGjlSzVM66:APA91bFTDlINnZ8NFvNQ-E9787DrWVnXIYOwuptpTVuV950i_pkqZoiN-mAMC4MNXZAe5IWy0OaLp4h2ALrtuugl-wQ57Wp8H9JApW_YUpilN93PfHAc2BXVFbOtn7ac4kLkWwTQei4d", + Status: 1, + Service: 2 }; + + this.http.post('https://equilibrium.dyndns.info/GabineteDigital.Services/V4/api/notifications/token', body,{headers}).subscribe(data => { + console.log('TOKEN USER MIDLE', data); + }) + /*this.http.get('http://gpr-dev-01.gabinetedigital.local/GabineteDigital.Services/V4/api/notifications/user/'+res).subscribe(data => { + console.log('TOKEN USER MIDLE',data); + })*/ + }); - + }); - + }, ); @@ -113,7 +130,7 @@ export class LoginPage implements OnInit { if (await this.authService.login(this.userattempt)) { /* this.loginRocketChat(); */ this.storeUserIdANdToken() - + this.router.navigate(['/home/events']); } else { diff --git a/src/app/services/notifications.service.spec.ts b/src/app/services/notifications.service.spec.ts new file mode 100644 index 000000000..c939c4c3f --- /dev/null +++ b/src/app/services/notifications.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { NotificationsService } from './notifications.service'; + +describe('NotificationsService', () => { + let service: NotificationsService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(NotificationsService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/notifications.service.ts b/src/app/services/notifications.service.ts new file mode 100644 index 000000000..c4374bea8 --- /dev/null +++ b/src/app/services/notifications.service.ts @@ -0,0 +1,30 @@ +import { Injectable } from '@angular/core'; +import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; +import { environment } from 'src/environments/environment'; +import { Token } from '../models/token.model'; + +@Injectable({ + providedIn: 'root' +}) +export class NotificationsService { + + constructor(private http: HttpClient,) { } + + getTokenByUserIdAndId(user, userID) { + const geturl = environment.apiURL + 'notifications/user/' + userID; + + return this.http.get(`${geturl}`); + } + + postToken(userId, token) { + const geturl = environment.apiURL + 'notifications/token'; + + let data = { + UserId: userId, + TokenId: token, + Status: 1, + Service: 2 + } + return this.http.post(`${geturl}`,data); + } +}