Improve user type

This commit is contained in:
Peter Maquiran
2021-05-10 14:51:51 +01:00
parent 6c5a5708e6
commit f846d50223
2 changed files with 47 additions and 25 deletions
+17 -19
View File
@@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
import { StorageService } from './storage.service';
import { Router } from '@angular/router';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { User } from '../models/user.model';
import { User, UserForm } from '../models/user.model';
import { environment } from 'src/environments/environment';
import { HttpService } from './http.service';
import { BehaviorSubject, Observable } from 'rxjs';
@@ -15,25 +15,26 @@ export class AuthService {
userData$ = new BehaviorSubject<any>('');
userId$ = new BehaviorSubject<any>('');
headers: HttpHeaders;
public ValidatedUser:User;
opts:any;
constructor(
private http: HttpClient,
private httpService: HttpService,
private storageService:StorageService,
private router:Router
) {
this.headers = new HttpHeaders();
//if(!environment.production){
if (localStorage.getItem("user") != null) {
this.ValidatedUser = JSON.parse(localStorage.getItem('user'));
}
//}
if (localStorage.getItem("user") != null) {
this.ValidatedUser = JSON.parse(localStorage.getItem('user'));
}
}
public ValidatedUser:User;
async login(user: User): Promise<boolean> {
async login(user: UserForm): Promise<boolean> {
user.BasicAuthKey = 'Basic ' + btoa(user.username + '@' + user.domainName + ':' + user.password); //conversão em base64 das credenciais inseridas
@@ -48,20 +49,17 @@ export class AuthService {
let response: any;
result = await this.http.get<boolean>(service, options).toPromise();
response = await this.http.post<any>(environment.apiURL + "UserAuthentication/Login", '', this.opts).toPromise();
response = await this.http.post<User>(environment.apiURL + "UserAuthentication/Login", '', this.opts).toPromise();
if (result)
{
this.ValidatedUser = user;
console.log(user);
console.log(response)
//if(!environment.production){
localStorage.setItem('user', JSON.stringify(Object.assign(user, response)));
//}
if (result) {
this.ValidatedUser = response;
this.storageService.store(AuthConnstants.USER, response);
}
localStorage.setItem('user', JSON.stringify(response));
this.storageService.store(AuthConnstants.USER, response);
}
return result;
}