@@ -10,59 +10,63 @@ import { AuthenticationService } from '@app/authentication/service/authenticatio
1010} )
1111export class LoginFormComponent implements OnInit {
1212
13- loginForm : FormGroup ;
14- loading = false ;
15- submitted = false ;
16- returnUrl : string = '' ;
17- error = '' ;
13+ public form = this . formBuilder . group ( {
14+ username : [ '' , Validators . required ] ,
15+ password : [ '' , Validators . required ]
16+ } ) ;
17+
18+ public loading = false ;
19+
20+ public submitted = false ;
21+
22+ public error = '' ;
23+
24+ private returnUrl : string = '' ;
1825
1926 constructor (
2027 private formBuilder : FormBuilder ,
2128 private route : ActivatedRoute ,
2229 private router : Router ,
2330 private authenticationService : AuthenticationService
24- ) {
31+ ) { }
32+
33+ ngOnInit ( ) {
2534 // redirect to home if already logged in
2635 if ( this . authenticationService . getUser ( ) . logged ) {
2736 this . router . navigate ( [ '/' ] ) ;
2837 }
29-
30- this . loginForm = this . formBuilder . group ( {
31- username : [ '' , Validators . required ] ,
32- password : [ '' , Validators . required ]
33- } ) ;
34- }
35-
36- ngOnInit ( ) {
3738 // get return url from route parameters or default to '/'
3839 this . returnUrl = this . route . snapshot . queryParams [ 'returnUrl' ] || '/' ;
3940 }
4041
41- // convenience getter for easy access to form fields
42- get f ( ) { return this . loginForm . controls ; }
43-
44- onSubmit ( ) {
42+ public onSubmit ( ) {
4543 this . submitted = true ;
4644
4745 // stop here if form is invalid
48- if ( this . loginForm . invalid ) {
49- return ;
46+ if ( this . form . valid ) {
47+ this . loading = true ;
48+ this . authenticationService . login ( this . form . value . username , this . form . value . password )
49+ . subscribe ( {
50+ next : user => {
51+ this . loading = false ;
52+ if ( user . logged ) {
53+ this . router . navigate ( [ this . returnUrl ] ) ;
54+ }
55+ } ,
56+ error : error => {
57+ this . error = error ;
58+ this . loading = false ;
59+ }
60+ } ) ;
5061 }
62+ }
5163
52- this . loading = true ;
53- this . authenticationService . login ( this . f [ 'username' ] . value , this . f [ 'password' ] . value )
54- . subscribe ( {
55- next : user => {
56- this . loading = false ;
57- if ( user . logged ) {
58- this . router . navigate ( [ this . returnUrl ] ) ;
59- }
60- } ,
61- error : error => {
62- this . error = error ;
63- this . loading = false ;
64- }
65- } ) ;
64+ public isFormInvalid ( ) : boolean {
65+ return this . form . invalid && ( this . form . dirty || this . form . touched ) ;
66+ }
67+
68+ public canLogin ( ) : boolean {
69+ return this . form . valid ;
6670 }
6771
6872}
0 commit comments