1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| export class LoginComponent implements OnInit {
loginForm: FormGroup;
submitted = false;
returnUrl : string;
error : string;
errorMessage : string;
subLogin : Subscription;
subNavigation: Subscription;
loggedUser : UserAccount;
navigation : Navigation[];
@Output() cancelEvent = new EventEmitter<boolean>();
constructor(private authService: AuthService,
private route : ActivatedRoute,
private router: Router,
private formBuilder: FormBuilder) {
this.loginForm = new FormGroup({
login: new FormControl(),
password: new FormControl()
});
}
ngOnInit() {
this.getFormFields();
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
}
get f() { return this.loginForm.controls; }
onSubmit() {
this.subLogin = this.authService.LogIn(this.f.login.value, this.f.password.value).subscribe((user: UserAccount) => {
user.isLogged = true;
this.loggedUser = user;
if(Object.keys(this.loggedUser).length!= 0){
let userAccountTypeId = this.loggedUser.userAccountTypeId.valueOf();
let truckId = this.loggedUser.truckId?.valueOf();
this.subNavigation = this.authService.getNavigation(userAccountTypeId).subscribe((navigation: Navigation[]) =>{
this.loggedUser.navigation = navigation;
this.router.navigate([`/useraccount/${truckId}`]);
});
}
else{
this.subNavigation = this.authService.getNavigation(4).subscribe((navigation: Navigation[]) =>{
this.loggedUser.navigation = navigation;
});
}
this.authService.updateAndEmitCurrentUser(user);
this.cancelEvent.emit(true);
}
);
}
getFormFields(){
this.loginForm = this.formBuilder.group({
login: ['', Validators.required],
password: ['', Validators.required]
});
}
cancel() {
this.cancelEvent.emit(true);
}
ngOnDestroy(): void {
if (this.subLogin) {
this.subLogin.unsubscribe();
}
}
} |