From 1175c00790945b28d9195874c5ae5bda7d6d1859 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=20Ku=CC=88ng?= Date: Sat, 6 May 2023 13:16:01 +0200 Subject: [PATCH] partial fix login error message --- src/app/component/login/login.component.ts | 17 ++++++++++++++--- .../management/management.component.ts | 10 ++++++++-- src/app/service/http/user.service.ts | 16 ++++++++++++++-- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/app/component/login/login.component.ts b/src/app/component/login/login.component.ts index 620afd5..3d6c50a 100644 --- a/src/app/component/login/login.component.ts +++ b/src/app/component/login/login.component.ts @@ -37,6 +37,10 @@ export class LoginComponent implements OnInit { createNewUser(createUser: UserEntity) { console.log(createUser.username+" "+createUser.password); + if (createUser.username == '' || createUser.password == '') { + this.errorCreateUser = true; + return; + } this.userService.getUsers().subscribe(users => { this.users = users; for (let i = 0; i < this.users.length; i++) { @@ -55,9 +59,14 @@ export class LoginComponent implements OnInit { loginFunction(username: string, password: string) { this.userService.getUser(username).subscribe(user => { - if (user.password == password) { - this.cookieService.setUsername(username); - this.router.navigate(['/home']); + console.log(user); + if (user !== null) { + if (user.password == password) { + this.cookieService.setUsername(username); + this.router.navigate(['/home']); + }else { + this.errorLogin = true; + } } else { this.errorLogin = true; } @@ -73,6 +82,8 @@ export class LoginComponent implements OnInit { } switch() { + this.errorCreateUser = false; + this.errorLogin = false; if (this.login) { this.login = false; } else { diff --git a/src/app/component/management/management.component.ts b/src/app/component/management/management.component.ts index 1400946..5910a32 100644 --- a/src/app/component/management/management.component.ts +++ b/src/app/component/management/management.component.ts @@ -49,9 +49,15 @@ export class ManagementComponent implements OnInit { this.translations = this.readTranslationJsonService.getManagementTranslations(); this.username = this.cookieService.getUsername(); this.userService.getUser(this.username).subscribe(user => { - if (user.admin == false) { - this.route.navigate(['/home']); + if (user !== null) { + if (user.admin == false) { + this.route.navigate(['/home']); + } + }else { + this.cookieService.deleteUsername(); + this.route.navigate(['/login']); } + }); this.locationService.getLocations().subscribe(locations => { this.locations = locations; diff --git a/src/app/service/http/user.service.ts b/src/app/service/http/user.service.ts index 01f40d7..26bb379 100644 --- a/src/app/service/http/user.service.ts +++ b/src/app/service/http/user.service.ts @@ -1,6 +1,7 @@ import {Injectable} from "@angular/core"; import {HttpClient} from "@angular/common/http"; import {UserEntity} from "../../interface/UserEntity"; +import {catchError, of} from "rxjs"; const BASE_URL = "progetto152"; const USER = BASE_URL + "/user"; @@ -21,16 +22,27 @@ export class UserService { return this.http.get(USER); } + getUser(username: string) { - return this.http.get(USER + "/" + username); + return this.http.get(USER + "/" + username).pipe( + catchError(error => { + if (error.status === 404) { + return of(null); + } else { + throw error; + } + }) + ); } + + getUserById(id: number) { return this.http.get(GET_USER_BY_ID + "/" + id); } createUser(user: UserEntity) { - console.log("create "+user); + console.log("create " + user); return this.http.post(USER, user); }