|
|
|
@ -1,23 +1,26 @@
|
|
|
|
|
import { Component } from '@angular/core';
|
|
|
|
|
import { Component, Renderer2 } from '@angular/core';
|
|
|
|
|
import { LoginService } from '../../services/login.service';
|
|
|
|
|
import { FormsModule, ReactiveFormsModule, Validators } from '@angular/forms';
|
|
|
|
|
import { FormBuilder } from '@angular/forms';
|
|
|
|
|
import { Router } from '@angular/router';
|
|
|
|
|
import { FormGroup } from '@angular/forms';
|
|
|
|
|
import { User } from '../../model/User';
|
|
|
|
|
import { LocalStorageService } from '../../services/localstorage.service';
|
|
|
|
|
import { NgIf } from '@angular/common';
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'app-login-page',
|
|
|
|
|
imports: [FormsModule, ReactiveFormsModule],
|
|
|
|
|
imports: [FormsModule, ReactiveFormsModule, NgIf],
|
|
|
|
|
templateUrl: './login-page.component.html',
|
|
|
|
|
styleUrl: './login-page.component.css'
|
|
|
|
|
})
|
|
|
|
|
export class LoginPageComponent {
|
|
|
|
|
|
|
|
|
|
userForm: FormGroup;
|
|
|
|
|
user: User = {login: '', password: ''}
|
|
|
|
|
user: User = {login: '', password: ''};
|
|
|
|
|
errorMessage: string = '';
|
|
|
|
|
|
|
|
|
|
constructor(private loginService: LoginService, private fb: FormBuilder, private router: Router) {
|
|
|
|
|
constructor(private loginService: LoginService, private fb: FormBuilder, private router: Router, private localStorageService: LocalStorageService, private renderer: Renderer2) {
|
|
|
|
|
this.userForm = this.fb.group({
|
|
|
|
|
login: [this.user.login, [Validators.required, Validators.minLength(3)]],
|
|
|
|
|
password: [this.user.password, [Validators.required, Validators.minLength(3)]],
|
|
|
|
@ -26,16 +29,35 @@ export class LoginPageComponent {
|
|
|
|
|
|
|
|
|
|
public login(){
|
|
|
|
|
if (this.userForm.invalid){
|
|
|
|
|
this.errorMessage = "Veuillez remplir tous les champs";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let login = this.userForm.value.login;
|
|
|
|
|
let password = this.userForm.value.password
|
|
|
|
|
|
|
|
|
|
this.user.login = login;
|
|
|
|
|
this.user.password = password;
|
|
|
|
|
this.user.login = this.userForm.value.login;
|
|
|
|
|
this.user.password = this.userForm.value.password;
|
|
|
|
|
|
|
|
|
|
this.loginService.login(this.user.login, this.user.password).subscribe({
|
|
|
|
|
next: (response) => {
|
|
|
|
|
console.log("Connexion OK: ", response);
|
|
|
|
|
this.localStorageService.setToken(response.access_token);
|
|
|
|
|
this.closeModal();
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.router.navigate(['/map']);
|
|
|
|
|
}, 500);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
error: (response) => {
|
|
|
|
|
console.log("Connexion KO: ", response.error.detail);
|
|
|
|
|
this.errorMessage = response.error.detail;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log(this.loginService.login(login,password));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private closeModal() {
|
|
|
|
|
const modal = document.getElementById('close-login-modal');
|
|
|
|
|
if (modal) {
|
|
|
|
|
modal.click();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|