This commit is contained in:
tiago.kayaya
2021-02-24 16:39:11 +01:00
parent fe2f3bccad
commit 5b0e22e427
6 changed files with 98 additions and 41 deletions
+72 -11
View File
@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { StorageService } from './storage.service';
/* import { StorageService } from './storage.service'; */
import { Router } from '@angular/router';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { User } from '../models/user.model';
@@ -7,6 +7,9 @@ import { environment } from 'src/environments/environment';
import { HttpService } from './http.service';
import { BehaviorSubject, Observable } from 'rxjs';
import { AuthConnstants } from '../config/auth-constants';
import { AlertService } from './alert.service';
import { map } from 'rxjs/operators';
import { Storage } from '@ionic/storage';
@Injectable({
providedIn: 'root'
@@ -16,18 +19,23 @@ export class AuthService {
userId$ = new BehaviorSubject<any>('');
headers: HttpHeaders;
opts:any;
rs:any;
constructor(
private http: HttpClient,
private httpService: HttpService,
private storageService:StorageService,
private router:Router
/* private storageService:StorageService, */
private router:Router,
private alertCrontroller: AlertService,
private storage: Storage,
) {
this.headers = new HttpHeaders();
}
public ValidatedUser:User;
public ValidatedUser2:any;
async login(user: User): Promise<boolean> {
this.alertCrontroller.presentAlert('1');
user.BasicAuthKey = 'Basic ' + btoa(user.username + '@' + user.domainName + ':' + user.password); //conversão em base64 das credenciais inseridas
const options = { headers: {'Authorization': user.BasicAuthKey }};
@@ -40,23 +48,76 @@ export class AuthService {
let result: boolean | PromiseLike<boolean>;
let response: any;
result = await this.http.get<boolean>(service, options).toPromise();
this.alertCrontroller.presentAlert('2');
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');
var that = this;
await this.http.get<boolean>(service, options).subscribe(res=>{
this.alertCrontroller.presentAlert('3.1');
result = res;
this.storage.set('login', res);
return result;
if (result)
{
this.alertCrontroller.presentAlert('4');
this.ValidatedUser = user;
//this.storageService.store(AuthConnstants.USER, response);
}
});
this.storage.get('name').then((val) => {
console.log('Your age is', val);
});
this.alertCrontroller.presentAlert('3');
response = await this.http.post<any>(environment.apiURL + "UserAuthentication/Login", '', this.opts).toPromise();
console.log(response);
/* this.http.post(environment.apiURL + "UserAuthentication/Login", '', this.opts).subscribe(res=>{
this.storageService.store(AuthConnstants.USER, res);
this.ValidatedUser = user;
response = user;
console.log(user);
}); */
if (result)
{
this.ValidatedUser = user;
//this.storageService.store(AuthConnstants.USER, response);
}
return result;
/* this.rs = this.storage.get('name').toPromise()
.then((val) => {
return val;
}); */
let t = this.get('name');
console.log(this.ValidatedUser2);
console.log(this.rs.map(responce=>responce.json()).toPromise());
return false;
}
async get(key: string): Promise<any> {
try {
const result = await this.storage.get(key);
console.log('storageGET: ' + key + ': ' + result);
if (result != null) {
this.ValidatedUser2 = result;
return result;
}
return null;
} catch (reason) {
console.log(reason);
return null;
}
}
logout(){
this.ValidatedUser = null;
}