Files
DiunaBI/Frontend/src/app/auth/auth.service.ts

61 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-12-06 12:27:09 +01:00
import { HttpClient, HttpHeaders } from '@angular/common/http';
2022-12-05 14:20:34 +01:00
import { Injectable } from '@angular/core';
2022-12-06 12:27:09 +01:00
import { environment } from 'src/environments/environment';
import { User } from '../models/user';
2022-12-05 14:20:34 +01:00
@Injectable({
providedIn: 'root'
})
2022-12-06 12:27:09 +01:00
export class AuthService {
2022-12-05 14:20:34 +01:00
2022-12-06 12:27:09 +01:00
apiToken: string | null = null;
user: User | null = null;
constructor(
private http$: HttpClient
) { }
loadDbUser() {
return new Promise((resolve, reject) => {
const headers = new HttpHeaders({
// 'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiToken}`
})
this.http$.get<any>(`${environment.api.url}/ping/ping`, {
headers,
withCredentials: true
}).subscribe({
next: (data) => {
console.log('Ping', data);
resolve(data);
},
error: (e) => {
console.error('Ping error', e);
reject(e);
}
}
);
});
}
getAPIToken(credentials: string): Promise<void> {
return new Promise((resolve, reject) => {
const header = new HttpHeaders().set('Content-type', 'application/json');
this.http$.post<any>(`${environment.api.url}/auth/apiToken`, JSON.stringify(credentials), { headers: header }).subscribe({
next: (data) => {
console.log('apiToken', data);
this.apiToken = data.token;
resolve(data);
},
error: (e) => {
console.error('apiToken error', e);
reject(e);
}
}
);
});
//const header = new HttpHeaders().set('Content-type', 'application/json');
//return this.httpClient.post(this.path + "LoginWithGoogle", JSON.stringify(credentials), { headers: header });
}
2022-12-05 14:20:34 +01:00
}