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

51 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-12-19 18:36:57 +01:00
import { HttpClient, HttpErrorResponse, 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(
2022-12-19 18:36:57 +01:00
private http$: HttpClient,
2022-12-06 12:27:09 +01:00
) { }
2022-12-09 00:14:05 +01:00
ping() {
2022-12-06 12:27:09 +01:00
return new Promise((resolve, reject) => {
2023-01-06 11:55:01 +01:00
this.http$.get<string>(`${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');
2023-01-06 11:55:01 +01:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2022-12-19 18:36:57 +01:00
this.http$.post<any>(`${environment.api.url}/auth/apiToken`, JSON.stringify(credentials), { headers: header }).subscribe({
2022-12-06 12:27:09 +01:00
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);
},
2022-12-19 18:36:57 +01:00
error: (e: HttpErrorResponse) => {
2022-12-06 12:27:09 +01:00
reject(e);
}
}
);
});
}
2022-12-05 14:20:34 +01:00
}