From 22f6d316b44efa179dfe8847cc4c03c6264031ab Mon Sep 17 00:00:00 2001 From: MiharyR Date: Thu, 13 Jan 2022 12:13:04 +0100 Subject: [PATCH] connexion avec le backend pour la page login --- frontend/src/app/app.module.ts | 2 ++ .../services/message/message.service.ts | 33 +++++++++++++++++-- .../common/services/profil/profil.service.ts | 31 +++++++++++++++-- .../login/page-login/page-login.component.ts | 27 +++++++-------- frontend/src/environments/environment.ts | 3 +- 5 files changed, 78 insertions(+), 18 deletions(-) diff --git a/frontend/src/app/app.module.ts b/frontend/src/app/app.module.ts index c87f35c..ebfbd59 100644 --- a/frontend/src/app/app.module.ts +++ b/frontend/src/app/app.module.ts @@ -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, diff --git a/frontend/src/app/common/services/message/message.service.ts b/frontend/src/app/common/services/message/message.service.ts index b22bacf..e67dc5a 100644 --- a/frontend/src/app/common/services/message/message.service.ts +++ b/frontend/src/app/common/services/message/message.service.ts @@ -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 + { + const urlComplete = environment.debutUrl + url ; + return this.http.post(urlComplete, data, {withCredentials: true}); + } + + get(url: string, params:HttpParams = new HttpParams()): Observable + { + const urlComplete = environment.debutUrl + url ; + return this.http.get(urlComplete,{ withCredentials: true, params: params }); + } + + put(url: string, data: any): Observable + { + const urlComplete = environment.debutUrl + url ; + return this.http.put(urlComplete, data, {withCredentials: true}); + } + + delete(url: string): Observable + { + const urlComplete = environment.debutUrl + url ; + return this.http.delete(urlComplete,{withCredentials: true}); + } - constructor() { } } diff --git a/frontend/src/app/common/services/profil/profil.service.ts b/frontend/src/app/common/services/profil/profil.service.ts index 213c1f3..0d209c0 100644 --- a/frontend/src/app/common/services/profil/profil.service.ts +++ b/frontend/src/app/common/services/profil/profil.service.ts @@ -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() { } } diff --git a/frontend/src/app/login/page-login/page-login.component.ts b/frontend/src/app/login/page-login/page-login.component.ts index 0076521..6dbfc62 100644 --- a/frontend/src/app/login/page-login/page-login.component.ts +++ b/frontend/src/app/login/page-login/page-login.component.ts @@ -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'); } } diff --git a/frontend/src/environments/environment.ts b/frontend/src/environments/environment.ts index f56ff47..d219c15 100644 --- a/frontend/src/environments/environment.ts +++ b/frontend/src/environments/environment.ts @@ -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/" }; /*