2020-08-06 14:31:07 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
2021-02-24 16:39:11 +01:00
|
|
|
/* import { StorageService } from './storage.service'; */
|
2020-08-06 14:31:07 +01:00
|
|
|
import { Router } from '@angular/router';
|
2020-10-30 15:22:35 +01:00
|
|
|
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
|
2020-08-21 00:22:51 +01:00
|
|
|
import { User } from '../models/user.model';
|
|
|
|
|
import { environment } from 'src/environments/environment';
|
2020-10-30 15:22:35 +01:00
|
|
|
import { HttpService } from './http.service';
|
|
|
|
|
import { BehaviorSubject, Observable } from 'rxjs';
|
|
|
|
|
import { AuthConnstants } from '../config/auth-constants';
|
2021-02-24 16:39:11 +01:00
|
|
|
import { AlertService } from './alert.service';
|
|
|
|
|
import { map } from 'rxjs/operators';
|
|
|
|
|
import { Storage } from '@ionic/storage';
|
2020-08-06 14:31:07 +01:00
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class AuthService {
|
2020-10-30 15:22:35 +01:00
|
|
|
userData$ = new BehaviorSubject<any>('');
|
2021-01-19 10:44:55 +01:00
|
|
|
userId$ = new BehaviorSubject<any>('');
|
|
|
|
|
headers: HttpHeaders;
|
|
|
|
|
opts:any;
|
2021-02-24 16:39:11 +01:00
|
|
|
rs:any;
|
2020-08-06 14:31:07 +01:00
|
|
|
constructor(
|
2020-10-30 15:22:35 +01:00
|
|
|
private http: HttpClient,
|
|
|
|
|
private httpService: HttpService,
|
2021-02-24 16:39:11 +01:00
|
|
|
/* private storageService:StorageService, */
|
|
|
|
|
private router:Router,
|
|
|
|
|
private alertCrontroller: AlertService,
|
|
|
|
|
private storage: Storage,
|
2021-01-19 10:44:55 +01:00
|
|
|
) {
|
|
|
|
|
this.headers = new HttpHeaders();
|
|
|
|
|
}
|
2020-08-06 14:31:07 +01:00
|
|
|
|
2020-08-21 00:22:51 +01:00
|
|
|
public ValidatedUser:User;
|
2020-08-06 14:31:07 +01:00
|
|
|
|
2021-02-24 17:23:35 +01:00
|
|
|
async login(user: User) {
|
2021-02-24 16:39:11 +01:00
|
|
|
this.alertCrontroller.presentAlert('1');
|
2021-01-25 11:02:56 +01:00
|
|
|
user.BasicAuthKey = 'Basic ' + btoa(user.username + '@' + user.domainName + ':' + user.password); //conversão em base64 das credenciais inseridas
|
2020-08-06 14:31:07 +01:00
|
|
|
|
2020-08-21 00:22:51 +01:00
|
|
|
const options = { headers: {'Authorization': user.BasicAuthKey }};
|
2021-01-19 10:44:55 +01:00
|
|
|
this.headers = this.headers.set('Authorization',user.BasicAuthKey);
|
|
|
|
|
this.opts = {
|
|
|
|
|
headers: this.headers,
|
|
|
|
|
}
|
2020-08-21 00:22:51 +01:00
|
|
|
const service = environment.apiURL + "userauthentication/GetValidateAuth";
|
|
|
|
|
|
|
|
|
|
let result: boolean | PromiseLike<boolean>;
|
2021-01-19 10:44:55 +01:00
|
|
|
let response: any;
|
2020-08-21 00:22:51 +01:00
|
|
|
|
2021-02-24 17:23:35 +01:00
|
|
|
/* this.alertCrontroller.presentAlert('2');
|
2021-02-24 16:39:11 +01:00
|
|
|
this.http.get('http://jsonplaceholder.typicode.com/todos/1');
|
|
|
|
|
this.alertCrontroller.presentAlert('2.1');
|
|
|
|
|
this.http.get('https://jsonplaceholder.typicode.com/todos/1');
|
|
|
|
|
this.alertCrontroller.presentAlert('2.2');
|
2021-02-24 17:23:35 +01:00
|
|
|
var that = this; */
|
2021-02-24 16:39:11 +01:00
|
|
|
|
|
|
|
|
await this.http.get<boolean>(service, options).subscribe(res=>{
|
2021-02-24 17:23:35 +01:00
|
|
|
//this.alertCrontroller.presentAlert('3.1');
|
2021-02-24 16:39:11 +01:00
|
|
|
result = res;
|
|
|
|
|
this.storage.set('login', res);
|
2021-02-24 17:23:35 +01:00
|
|
|
if (result){
|
|
|
|
|
//this.alertCrontroller.presentAlert('4');
|
2021-02-24 16:39:11 +01:00
|
|
|
this.ValidatedUser = user;
|
|
|
|
|
//this.storageService.store(AuthConnstants.USER, response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2021-02-24 17:23:35 +01:00
|
|
|
//this.alertCrontroller.presentAlert('3');
|
2021-01-25 11:02:56 +01:00
|
|
|
response = await this.http.post<any>(environment.apiURL + "UserAuthentication/Login", '', this.opts).toPromise();
|
2021-02-24 16:39:11 +01:00
|
|
|
console.log(response);
|
|
|
|
|
|
2021-01-25 11:02:56 +01:00
|
|
|
/* this.http.post(environment.apiURL + "UserAuthentication/Login", '', this.opts).subscribe(res=>{
|
2021-01-19 10:44:55 +01:00
|
|
|
this.storageService.store(AuthConnstants.USER, res);
|
2021-01-25 11:02:56 +01:00
|
|
|
this.ValidatedUser = user;
|
|
|
|
|
response = user;
|
|
|
|
|
console.log(user);
|
|
|
|
|
}); */
|
2021-02-24 16:39:11 +01:00
|
|
|
|
|
|
|
|
/* this.rs = this.storage.get('name').toPromise()
|
|
|
|
|
.then((val) => {
|
|
|
|
|
return val;
|
|
|
|
|
}); */
|
|
|
|
|
|
2020-08-21 00:22:51 +01:00
|
|
|
}
|
2020-08-06 14:31:07 +01:00
|
|
|
|
2021-02-24 16:39:11 +01:00
|
|
|
async get(key: string): Promise<any> {
|
|
|
|
|
try {
|
|
|
|
|
const result = await this.storage.get(key);
|
|
|
|
|
console.log('storageGET: ' + key + ': ' + result);
|
|
|
|
|
if (result != null) {
|
2021-02-24 17:23:35 +01:00
|
|
|
return result;
|
2021-02-24 16:39:11 +01:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
} catch (reason) {
|
|
|
|
|
console.log(reason);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 00:22:51 +01:00
|
|
|
logout(){
|
|
|
|
|
this.ValidatedUser = null;
|
2020-10-30 15:22:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Login to rocketChat server
|
2020-12-16 15:07:28 +01:00
|
|
|
loginChat(postData: any):Observable<any> {
|
2021-01-26 11:03:00 +01:00
|
|
|
console.log(postData);
|
|
|
|
|
|
2021-01-13 10:02:30 +01:00
|
|
|
return this.httpService.post('login', postData);
|
2020-10-30 15:22:35 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-13 10:02:30 +01:00
|
|
|
//Get user data from RocketChat | global object
|
2020-12-16 16:06:14 +01:00
|
|
|
getUserData(){
|
2021-02-23 16:10:14 +01:00
|
|
|
/* this.storageService.get(AuthConnstants.AUTH).then(res=>{
|
2020-10-30 15:22:35 +01:00
|
|
|
this.userData$.next(res);
|
2021-02-23 16:10:14 +01:00
|
|
|
}); */
|
2020-12-16 16:06:14 +01:00
|
|
|
}
|
2021-01-19 10:44:55 +01:00
|
|
|
//Get user Id | global object
|
|
|
|
|
getUserId(){
|
2021-02-23 16:10:14 +01:00
|
|
|
/* this.storageService.get(AuthConnstants.USER).then(res=>{
|
2021-01-19 10:44:55 +01:00
|
|
|
this.userId$.next(res);
|
2021-02-23 16:10:14 +01:00
|
|
|
}); */
|
2021-01-19 10:44:55 +01:00
|
|
|
}
|
2020-10-30 15:22:35 +01:00
|
|
|
|
2021-01-13 10:02:30 +01:00
|
|
|
getProfile(){
|
2021-02-23 16:10:14 +01:00
|
|
|
/* this.storageService.get(AuthConnstants.PROFILE).then(res=>{
|
2020-11-24 13:46:13 +01:00
|
|
|
return res;
|
2021-02-23 16:10:14 +01:00
|
|
|
}); */
|
2021-01-13 10:02:30 +01:00
|
|
|
}
|
2020-11-24 13:46:13 +01:00
|
|
|
|
2020-10-30 15:22:35 +01:00
|
|
|
logoutChat(){
|
|
|
|
|
//this.storageService.clear();
|
2021-02-23 16:10:14 +01:00
|
|
|
/* this.storageService.removeStorageItem(AuthConnstants.AUTH).then(res =>{
|
2020-10-30 15:22:35 +01:00
|
|
|
this.userData$.next('');
|
|
|
|
|
this.router.navigate(['']);
|
2021-02-23 16:10:14 +01:00
|
|
|
}) */
|
2020-10-30 15:22:35 +01:00
|
|
|
|
2020-08-19 23:47:11 +01:00
|
|
|
}
|
2020-08-06 14:31:07 +01:00
|
|
|
}
|