2022-01-19 16:14:55 +01:00
|
|
|
import { HttpClient } from '@angular/common/http';
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { map, Observable } from 'rxjs';
|
|
|
|
import { environment } from 'src/environments/environment';
|
|
|
|
import { Membre } from '../models/membre';
|
2022-01-20 17:58:46 +01:00
|
|
|
import { Team } from '../models/team';
|
2022-01-19 16:14:55 +01:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root',
|
|
|
|
})
|
|
|
|
export class AuthService {
|
|
|
|
private apiUrl: string;
|
|
|
|
private tokenKey: string;
|
|
|
|
|
|
|
|
constructor(private http: HttpClient) {
|
|
|
|
// On se sert des variables d'environnement de notre application
|
|
|
|
this.apiUrl = environment.apiUrl;
|
|
|
|
this.tokenKey = environment.tokenKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
signup(membre: Membre): Observable<any> {
|
|
|
|
console.log(membre);
|
|
|
|
|
2022-01-20 17:58:18 +01:00
|
|
|
return this.http.post(`${this.apiUrl}/membres/sign-up`, membre);
|
2022-01-19 16:14:55 +01:00
|
|
|
}
|
|
|
|
|
2022-01-20 17:58:46 +01:00
|
|
|
|
2022-01-19 16:14:55 +01:00
|
|
|
signin(email: string, password: string): Observable<any> {
|
|
|
|
const body = {
|
|
|
|
email: email,
|
|
|
|
password: password,
|
|
|
|
};
|
|
|
|
|
|
|
|
console.log('Mon body : ', body);
|
|
|
|
|
|
|
|
// Modifier cette partie ci-dessous :
|
|
|
|
// - pour pouvoir stocker dans le localstorage notre accesstoken
|
|
|
|
// - Sous la clé "TOKEN-LBP"
|
|
|
|
|
2022-01-20 17:58:18 +01:00
|
|
|
return this.http.post(`${this.apiUrl}/membres/sign-in`, body).pipe(
|
2022-01-19 16:14:55 +01:00
|
|
|
map((x: any) => {
|
|
|
|
console.log('Service : ', x.accessToken);
|
|
|
|
// Modification à faire ici
|
|
|
|
localStorage.setItem(this.tokenKey, x.accessToken);
|
|
|
|
return x; // permet de renvoyer la réponse à l'initiateur (page Signin) après le traitement du map
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-20 17:58:18 +01:00
|
|
|
forgotPassword(email: string): Observable<any> {
|
2022-01-19 16:14:55 +01:00
|
|
|
const body = {
|
|
|
|
email: email,
|
|
|
|
};
|
2022-01-20 17:58:18 +01:00
|
|
|
return this.http.post(`${this.apiUrl}/membres/forgot-password`, body);
|
2022-01-19 16:14:55 +01:00
|
|
|
}
|
2022-01-20 17:58:46 +01:00
|
|
|
|
2022-01-21 09:45:22 +01:00
|
|
|
|
2022-01-20 17:58:46 +01:00
|
|
|
creationTeam(team: Team): Observable<any> {
|
|
|
|
console.log(team);
|
|
|
|
|
2022-01-21 12:56:22 +01:00
|
|
|
return this.http.post(`${this.apiUrl}/teams/add`, team);
|
2022-01-20 17:58:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-21 09:45:22 +01:00
|
|
|
|
2022-01-20 17:58:46 +01:00
|
|
|
addMember(membre: Membre): Observable<any> {
|
|
|
|
console.log(membre);
|
|
|
|
|
|
|
|
return this.http.post(`${this.apiUrl}/tableau-de-bord`, membre);
|
|
|
|
}
|
|
|
|
|
2022-01-19 16:14:55 +01:00
|
|
|
}
|