partial fix login error message

This commit is contained in:
2023-05-06 13:16:01 +02:00
parent 1690e73e0b
commit 1175c00790
3 changed files with 36 additions and 7 deletions

View File

@@ -37,6 +37,10 @@ export class LoginComponent implements OnInit {
createNewUser(createUser: UserEntity) { createNewUser(createUser: UserEntity) {
console.log(createUser.username+" "+createUser.password); console.log(createUser.username+" "+createUser.password);
if (createUser.username == '' || createUser.password == '') {
this.errorCreateUser = true;
return;
}
this.userService.getUsers().subscribe(users => { this.userService.getUsers().subscribe(users => {
this.users = users; this.users = users;
for (let i = 0; i < this.users.length; i++) { for (let i = 0; i < this.users.length; i++) {
@@ -55,9 +59,14 @@ export class LoginComponent implements OnInit {
loginFunction(username: string, password: string) { loginFunction(username: string, password: string) {
this.userService.getUser(username).subscribe(user => { this.userService.getUser(username).subscribe(user => {
if (user.password == password) { console.log(user);
this.cookieService.setUsername(username); if (user !== null) {
this.router.navigate(['/home']); if (user.password == password) {
this.cookieService.setUsername(username);
this.router.navigate(['/home']);
}else {
this.errorLogin = true;
}
} else { } else {
this.errorLogin = true; this.errorLogin = true;
} }
@@ -73,6 +82,8 @@ export class LoginComponent implements OnInit {
} }
switch() { switch() {
this.errorCreateUser = false;
this.errorLogin = false;
if (this.login) { if (this.login) {
this.login = false; this.login = false;
} else { } else {

View File

@@ -49,9 +49,15 @@ export class ManagementComponent implements OnInit {
this.translations = this.readTranslationJsonService.getManagementTranslations(); this.translations = this.readTranslationJsonService.getManagementTranslations();
this.username = this.cookieService.getUsername(); this.username = this.cookieService.getUsername();
this.userService.getUser(this.username).subscribe(user => { this.userService.getUser(this.username).subscribe(user => {
if (user.admin == false) { if (user !== null) {
this.route.navigate(['/home']); if (user.admin == false) {
this.route.navigate(['/home']);
}
}else {
this.cookieService.deleteUsername();
this.route.navigate(['/login']);
} }
}); });
this.locationService.getLocations().subscribe(locations => { this.locationService.getLocations().subscribe(locations => {
this.locations = locations; this.locations = locations;

View File

@@ -1,6 +1,7 @@
import {Injectable} from "@angular/core"; import {Injectable} from "@angular/core";
import {HttpClient} from "@angular/common/http"; import {HttpClient} from "@angular/common/http";
import {UserEntity} from "../../interface/UserEntity"; import {UserEntity} from "../../interface/UserEntity";
import {catchError, of} from "rxjs";
const BASE_URL = "progetto152"; const BASE_URL = "progetto152";
const USER = BASE_URL + "/user"; const USER = BASE_URL + "/user";
@@ -21,16 +22,27 @@ export class UserService {
return this.http.get<UserEntity[]>(USER); return this.http.get<UserEntity[]>(USER);
} }
getUser(username: string) { getUser(username: string) {
return this.http.get<UserEntity>(USER + "/" + username); return this.http.get<UserEntity>(USER + "/" + username).pipe(
catchError(error => {
if (error.status === 404) {
return of(null);
} else {
throw error;
}
})
);
} }
getUserById(id: number) { getUserById(id: number) {
return this.http.get<UserEntity>(GET_USER_BY_ID + "/" + id); return this.http.get<UserEntity>(GET_USER_BY_ID + "/" + id);
} }
createUser(user: UserEntity) { createUser(user: UserEntity) {
console.log("create "+user); console.log("create " + user);
return this.http.post<UserEntity>(USER, user); return this.http.post<UserEntity>(USER, user);
} }