diff --git a/src/app/config/auth-constants.ts b/src/app/config/auth-constants.ts index e6769cfd5..677d091cc 100644 --- a/src/app/config/auth-constants.ts +++ b/src/app/config/auth-constants.ts @@ -4,4 +4,5 @@ export class AuthConnstants{ public static readonly AUTH = 'userDataKey'; public static readonly PROFILE = 'profile'; public static readonly USER = 'userId'; + public static readonly USERCHAT = 'userChat'; } \ No newline at end of file diff --git a/src/app/pages/chat/chat.page.html b/src/app/pages/chat/chat.page.html index 2cfdd3cb6..62fa07099 100644 --- a/src/app/pages/chat/chat.page.html +++ b/src/app/pages/chat/chat.page.html @@ -51,7 +51,7 @@
- + {{user}} diff --git a/src/app/pages/chat/chat.page.ts b/src/app/pages/chat/chat.page.ts index f4b9c3df0..32a7371b3 100644 --- a/src/app/pages/chat/chat.page.ts +++ b/src/app/pages/chat/chat.page.ts @@ -90,6 +90,8 @@ export class ChatPage implements OnInit { /* Fim websockets variables*/ + loggedUserChat:any; + constructor( private http:HttpClient, private chatService: ChatService, @@ -98,11 +100,14 @@ export class ChatPage implements OnInit { private storage:Storage, private resolver: ComponentFactoryResolver, ){ + this.loggedUserChat = authService.ValidatedUserChat['data']; this.headers = new HttpHeaders(); this.loadMessage(); } ngOnInit() { + console.log(this.loggedUserChat); + this.segment = "Contactos"; this.authService.userData$.subscribe((res:any)=>{ @@ -255,7 +260,7 @@ sendMsg() { } getChatMembers(){ this.chatService.getMembers(this.userDirectMessages[0]._id).subscribe(res=> { - this.dmUsers = res['members'].filter(data => data.username != this.loggedUser.me.username) + this.dmUsers = res['members'].filter(data => data.username != this.loggedUserChat.me.username) }); } getGroups(){ diff --git a/src/app/pages/login/login.page.ts b/src/app/pages/login/login.page.ts index dec8ade5e..3280944bf 100644 --- a/src/app/pages/login/login.page.ts +++ b/src/app/pages/login/login.page.ts @@ -59,7 +59,6 @@ export class LoginPage implements OnInit { } else { this.userLoginPreference = '' } - } ngOnInit() {} @@ -72,7 +71,6 @@ export class LoginPage implements OnInit { ); } - async presentAlert(message: string) { const alert = await this.alertController.create({ cssClass: 'my-custom-class', @@ -84,7 +82,7 @@ export class LoginPage implements OnInit { await alert.present(); } - loginRocketChat() { + /* loginRocketChat() { let postData = { "user": this.username, @@ -99,7 +97,7 @@ export class LoginPage implements OnInit { console.log('Network error'); this.presentAlert('Network error ' + error); }); - } + } */ getToken() { // this.notificatinsservice.getAndpostToken(this.username); @@ -115,7 +113,8 @@ export class LoginPage implements OnInit { BasicAuthKey: "" } if (await this.authService.login(this.userattempt)) { - this.loginRocketChat(); + //this.loginRocketChat(); + this.authService.loginChat(this.userattempt); //this.getToken(); if(!this.pin) { diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index fd1ba8331..1585007f6 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -7,6 +7,7 @@ import { environment } from 'src/environments/environment'; import { HttpService } from './http.service'; import { BehaviorSubject, Observable } from 'rxjs'; import { AuthConnstants } from '../config/auth-constants'; +import { AlertController } from '@ionic/angular'; @Injectable({ providedIn: 'root' @@ -16,13 +17,15 @@ export class AuthService { userId$ = new BehaviorSubject(''); headers: HttpHeaders; public ValidatedUser:User; + public ValidatedUserChat:any; opts:any; constructor( private http: HttpClient, private httpService: HttpService, private storageService:StorageService, - private router:Router + private router:Router, + public alertController: AlertController, ) { this.headers = new HttpHeaders(); @@ -30,6 +33,9 @@ export class AuthService { if (localStorage.getItem("user") != null) { this.ValidatedUser = JSON.parse(localStorage.getItem('user')); } + if (localStorage.getItem("userChat") != null) { + this.ValidatedUserChat = JSON.parse(localStorage.getItem('userChat')); + } } @@ -75,12 +81,30 @@ export class AuthService { } //Login to rocketChat server - loginChat(postData: any):Observable { - console.log(postData); - - return this.httpService.post('login', postData); + async loginChat(user: UserForm): Promise { + let postData = { + "user": user.username, + "password": user.password, + } + + let responseChat = await this.httpService.post('login', postData).toPromise(); + if(responseChat){ + console.log('Login to Rocket chat OK'); + this.ValidatedUserChat = responseChat; + localStorage.setItem('userChat', JSON.stringify(responseChat)); + this.storageService.store(AuthConnstants.AUTH, responseChat); + return true; + } + else{ + console.log('Network error'); + this.presentAlert('Network error'); + return false; + } } + + + //Get user data from RocketChat | global object getUserData(){ this.storageService.get(AuthConnstants.AUTH).then(res=>{ @@ -108,4 +132,14 @@ export class AuthService { }) */ } + async presentAlert(message: string) { + const alert = await this.alertController.create({ + cssClass: 'my-custom-class', + header: 'Mensagem do sistema', + message: message, + buttons: ['OK'] + }); + + await alert.present(); + } } diff --git a/src/app/services/chat.service.ts b/src/app/services/chat.service.ts index c73767b2a..838225d47 100644 --- a/src/app/services/chat.service.ts +++ b/src/app/services/chat.service.ts @@ -22,7 +22,7 @@ export class ChatService { SERVER_URL = 'wss://www.tabularium.pt/websocket'; public messages: Subject; - + loggedUserChat:any; constructor( private http:HttpClient, @@ -33,11 +33,10 @@ export class ChatService { private wsService: WebsocketService, ) { + this.loggedUserChat = authService.ValidatedUserChat; this.headers = new HttpHeaders(); - this.authService.userData$.subscribe((res:any)=>{ - this.headers = this.headers.set('X-User-Id', res.userId); - this.headers = this.headers.set('X-Auth-Token', res.authToken); - }); + this.headers = this.headers.set('X-User-Id', this.loggedUserChat['data'].userId); + this.headers = this.headers.set('X-Auth-Token', this.loggedUserChat['data'].authToken); this.options = { headers: this.headers, }; @@ -48,25 +47,6 @@ export class ChatService { return (JSON.stringify(data)); }); } - - /* getUser(){ - this.storage.get('user').then((val) => { - let user = JSON.parse(unescape(atob(val))).data; - - this.headers = this.headers.set('X-User-Id', user.userId); - this.headers = this.headers.set('X-Auth-Token', user.authToken); - - this.options1 = { - headers: this.headers, - }; - console.log(this.options1); - - this.http.get(environment.apiChatUrl+'users.presence', this.options1).subscribe(res => { - console.log(res); - }); - - }); - } */ getAllChannels(){ return this.http.get(environment.apiChatUrl+'channels.list', this.options); @@ -108,7 +88,6 @@ export class ChatService { //Load messages from roomId getAllDirectMessages(){ - console.log(this.options); return this.http.get(environment.apiChatUrl+'im.list', this.options); } //Load messages from roomId