progress for login feature

This commit is contained in:
2023-05-04 15:46:33 +02:00
parent 2d0af1f90a
commit a5ad7ce97d
3 changed files with 75 additions and 9 deletions

View File

@@ -1,7 +1,7 @@
<div class="h-screen w-screen flex items-center justify-center"> <div class="h-screen w-screen flex items-center justify-center">
<div class="max-w-md py-12"> <div class="max-w-md py-12">
<div class="bg-white px-6 py-8 rounded-lg shadow-md w-[400px]"> <div class="bg-white px-6 py-8 rounded-lg shadow-md w-[400px]" *ngIf="login">
<h2 class="text-center text-2xl font-bold mb-8">Accedi</h2> <h2 class="text-center text-2xl font-bold mb-8">Accedi</h2>
@@ -27,5 +27,31 @@
</div> </div>
<div class="bg-white px-6 py-8 rounded-lg shadow-md w-[400px]" *ngIf="!login">
<h2 class="text-center text-2xl font-bold mb-8">Registrati</h2>
<form>
<div class="mb-4">
<label for="emails" class="block text-gray-700 font-bold mb-2">Email:</label>
<input type="emails" id="emails" name="email" placeholder="Inserisci username"
class="w-full px-4 py-2 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-600 focus:border-transparent">
</div>
<div class="mb-4">
<label for="passwords" class="block text-gray-700 font-bold mb-2">Password:</label>
<input type="passwords" id="passwords" name="password" placeholder="Inserisci la tua password"
class="w-full px-4 py-2 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-600 focus:border-transparent">
</div>
<div class="mb-4">
<button type="submit" class="btn gap-2 border-primary bg-primary text-secondary hover:bg-secondary hover:text-primary hover:border-primary py-2 w-full">Accedi</button>
</div>
</form>
</div>
</div> </div>
</div> </div>

View File

@@ -3,6 +3,8 @@ import {Router} from "@angular/router";
import {TranslateService} from "../../service/language/translate.service"; import {TranslateService} from "../../service/language/translate.service";
import {ReadTranslateJsonService} from "../../service/language/readTranslateJson.service"; import {ReadTranslateJsonService} from "../../service/language/readTranslateJson.service";
import {cookieService} from "../../service/cookie.service"; import {cookieService} from "../../service/cookie.service";
import {UserService} from "../../service/http/user.service";
import {UserEntity} from "../../interface/UserEntity";
@Component({ @Component({
selector: 'app-login', selector: 'app-login',
@@ -11,21 +13,61 @@ import {cookieService} from "../../service/cookie.service";
}) })
export class LoginComponent implements OnInit { export class LoginComponent implements OnInit {
newUser: { username: string; password: string; } = {username: '', password: ''};
username: string = '';
users: UserEntity[] | undefined;
login: boolean = true;
errorCreateUser: boolean = false;
errorLogin: boolean = false;
constructor( constructor(
private router: Router, private router: Router,
private translateService: TranslateService, private translateService: TranslateService,
private readTranslationJsonService: ReadTranslateJsonService, private readTranslationJsonService: ReadTranslateJsonService,
private userService: UserService,
private cookieService: cookieService, private cookieService: cookieService,
) { ) {
} }
ngOnInit(): void { ngOnInit(): void {
if (this.cookieService.getUsername() != null) {
this.router.navigate(['/home']);
}
} }
submit(username: string, password: string) { createNewUser(username: string, password: string) {
this.userService.getUsers().subscribe(users => {
this.users = users;
for (let i = 0; i < this.users.length; i++) {
if (this.users[i].username == username) {
this.errorCreateUser = true;
}
}
this.newUser.username = username;
this.newUser.password = password;
this.userService.createUser(this.newUser).subscribe(user => {
this.cookieService.setUsername(username);
this.router.navigate(['/management']);
});
});
} }
loginFunction(username: string, password: string) {
this.userService.getUser(username).subscribe(user => {
if (user.password == password) {
this.cookieService.setUsername(username);
this.router.navigate(['/management']);
} else {
this.errorLogin = true;
}
});
}
switch() {
if (this.login) {
this.login = false;
} else {
this.login = true;
}
}
} }

View File

@@ -1,8 +1,6 @@
export interface UserEntity { export interface UserEntity {
id?: number; id?: number;
name: string;
username: string; username: string;
password: string; password: string;
admin?: boolean;
} }