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

53 lines
1.4 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';
2022-12-11 23:40:16 +01:00
import { User } from '../models/user.model';
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-11 23:40:16 +01:00
apiToken!: string;
user!: User;
2022-12-06 12:27:09 +01:00
constructor(
private http$: HttpClient
) { }
2022-12-09 00:14:05 +01:00
ping() {
2022-12-06 12:27:09 +01:00
return new Promise((resolve, reject) => {
2022-12-11 21:30:54 +01:00
this.http$.get<any>(`${environment.api.url}/ping/ping`).subscribe({
2022-12-06 12:27:09 +01:00
next: (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) => {
2022-12-11 23:40:16 +01:00
this.user.id = data.id;
2022-12-06 12:27:09 +01:00
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
}