connexion avec le backend pour la page login

This commit is contained in:
MiharyR 2022-01-13 12:13:04 +01:00
parent 9566d11dd7
commit 22f6d316b4
5 changed files with 78 additions and 18 deletions

View file

@ -28,6 +28,7 @@ import { PageRegistryComponent } from './user/page-registry/page-registry.compon
import { PopupDeleteProfilComponent } from './common/components/popup-delete-profil/popup-delete-profil.component';
import {MatSortModule} from "@angular/material/sort";
import { PopupUpdatePersonAdminComponent } from './admin/userList/popup-update-person-admin/popup-update-person-admin.component';
import {HttpClientModule} from "@angular/common/http";
@ -51,6 +52,7 @@ import { PopupUpdatePersonAdminComponent } from './admin/userList/popup-update-p
AppRoutingModule,
FormsModule,
BrowserAnimationsModule,
HttpClientModule,
MatFormFieldModule,
MatInputModule,
MatButtonModule,

View file

@ -1,9 +1,38 @@
import { Injectable } from '@angular/core';
import {HttpClient, HttpParams} from "@angular/common/http";
import {Observable} from "rxjs";
import {environment} from "../../../../environments/environment";
@Injectable({
providedIn: 'root'
})
export class MessageService {
export class MessageService
{
constructor( private http: HttpClient ) { }
post(url: string, data: any): Observable<any>
{
const urlComplete = environment.debutUrl + url ;
return this.http.post<any>(urlComplete, data, {withCredentials: true});
}
get(url: string, params:HttpParams = new HttpParams()): Observable<any>
{
const urlComplete = environment.debutUrl + url ;
return this.http.get<any>(urlComplete,{ withCredentials: true, params: params });
}
put(url: string, data: any): Observable<any>
{
const urlComplete = environment.debutUrl + url ;
return this.http.put<any>(urlComplete, data, {withCredentials: true});
}
delete(url: string): Observable<any>
{
const urlComplete = environment.debutUrl + url ;
return this.http.delete<any>(urlComplete,{withCredentials: true});
}
constructor() { }
}

View file

@ -3,7 +3,34 @@ import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ProfilService {
export class ProfilService
{
getId(): number | null
{
let idString = localStorage.getItem('id');
if(idString === null) return null;
else return parseInt(idString);
}
setId(id: number): void
{
localStorage.setItem('id', id.toString());
}
getIsAdmin(): boolean
{
let isAdminString = localStorage.getItem('isAdmin');
if(isAdminString === "T") return true;
else return false;
}
setIsAdmin(isAdmin: boolean): void
{
let isAdminString = "" ;
if(isAdmin) isAdminString = "T";
else isAdminString = "F";
localStorage.setItem('isAdmin', isAdminString);
}
constructor() { }
}

View file

@ -1,7 +1,7 @@
import {Component} from '@angular/core';
import {Router} from "@angular/router";
import {MessageService} from "../../common/services/message/message.service";
import {HashageService} from "../../common/services/hashage/hashage.service";
import {ProfilService} from "../../common/services/profil/profil.service";
@ -20,7 +20,7 @@ export class PageLoginComponent
constructor( private messageService: MessageService,
private router: Router,
private hashageService: HashageService ) { }
private profilService: ProfilService ) { }
// Appuie sur le bouton "seConnecter"
@ -29,30 +29,31 @@ export class PageLoginComponent
this.checkField();
if(!this.hasError)
{
let data = {
const data = {
email: this.email,
hash_pass: this.hashageService.run(this.password)
password: this.password
};
console.log(data);
/*
this.messageService
.sendMessage('user/auth', data)
.subscribe( retour => this.callbackSeConnecter(retour))
*/
.post('login', data)
.subscribe( retour => this.onSeConnecterCallback(retour), err => this.onSeConnecterCallback(err));
}
}
// Callback de "onSeConnecter"
callbackSeConnecter(retour: any): void
onSeConnecterCallback(retour: any): void
{
if(retour.status !== 200)
console.log(retour);
if(retour.status !== "success")
{
this.errorMessage = retour.error.data.reason;
this.errorMessage = retour.message;
this.hasError = true;
}
else {
//this.router.navigateByUrl( '/search' );
this.profilService.setId(retour.data.id);
this.profilService.setIsAdmin(retour.data.is_admin)
if(!retour.data.is_admin) this.router.navigateByUrl('admin/userList');
else this.router.navigateByUrl('user/userList');
}
}

View file

@ -3,7 +3,8 @@
// The list of file replacements can be found in `angular.json`.
export const environment = {
production: false
production: false,
debutUrl: "http://127.0.0.1:5000/api/"
};
/*