WIP: backend protection
This commit is contained in:
@@ -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 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user