WIP: backend protection

This commit is contained in:
2022-12-06 12:27:09 +01:00
parent 7330fb90f2
commit 55b5150049
23 changed files with 499 additions and 114 deletions

View File

@@ -1,9 +1,60 @@
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
import { User } from '../models/user';
@Injectable({
providedIn: 'root'
})
export class AuthServiceService {
export class AuthService {
constructor() { }
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 });
}
}