82 lines
2.1 KiB
TypeScript
82 lines
2.1 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
|
import { Router, ActivatedRoute } from '@angular/router';
|
|
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
|
import { first } from 'rxjs/operators';
|
|
|
|
import { AuthenticationService } from '../user-services';
|
|
|
|
@Component({
|
|
templateUrl: './login.component.html',
|
|
styleUrls: ['./login.component.css'],
|
|
selector: 'login'
|
|
})
|
|
export class LoginComponent implements OnInit {
|
|
loginForm: FormGroup;
|
|
|
|
|
|
loading = false;
|
|
submitted = false;
|
|
returnUrl ="";
|
|
// get return url from route parameters or default to '/'
|
|
|
|
error = '';
|
|
|
|
constructor(
|
|
private formBuilder: FormBuilder,
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private authenticationService: AuthenticationService) {
|
|
|
|
this.loginForm = this.formBuilder.group({
|
|
username: ['', Validators.required],
|
|
password: ['', Validators.required]
|
|
});
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/home/concessionvalidation';
|
|
// reset login status
|
|
this.authenticationService.logout();
|
|
|
|
|
|
}
|
|
|
|
// convenience getter for easy access to form fields
|
|
get f() { return this.loginForm.value; }
|
|
|
|
onSubmit() {
|
|
this.submitted = true;
|
|
|
|
// stop here if form is invalid
|
|
if (this.loginForm.invalid) {
|
|
return;
|
|
}
|
|
|
|
const user = this.loginForm.value.username;
|
|
const pass = this.loginForm.value.password;
|
|
console.log("the form", this.loginForm, user,pass);
|
|
this.loading = true;
|
|
this.authenticationService.login(user,pass)
|
|
.pipe(first())
|
|
.subscribe({
|
|
next: data => {
|
|
this.loading = false;
|
|
console.log("login result ", data);
|
|
if (data == 1)
|
|
this.router.navigate([this.returnUrl]);
|
|
else
|
|
{
|
|
this.loginForm.patchValue({password: ''});
|
|
alert("Invalid Username or Password. Please try again.");
|
|
|
|
}
|
|
},
|
|
error: er => {
|
|
console.log("error in login", er);
|
|
this.error = er.message;
|
|
this.loading = false;
|
|
}
|
|
});
|
|
}
|
|
}
|