11import { Component } from '@angular/core' ;
2- import { AuthenticationTokenService } from '@app/authentication/service/authentication-token.service' ;
2+ import { FormBuilder , FormGroup , Validators } from '@angular/forms' ;
3+ import { ActivatedRoute , Router } from '@angular/router' ;
34import { AuthenticationService } from '@app/authentication/service/authentication.service' ;
5+ import { first } from 'rxjs/operators' ;
46
57@Component ( {
68 selector : 'app-login-form' ,
@@ -9,43 +11,56 @@ import { AuthenticationService } from '@app/authentication/service/authenticatio
911} )
1012export class LoginFormComponent {
1113
12- form : any = {
13- username : null ,
14- password : null
15- } ;
14+ loginForm : FormGroup ;
15+ loading = false ;
16+ submitted = false ;
17+ returnUrl : string = '' ;
18+ error = '' ;
1619
17- isLoggedIn = false ;
18- isLoginFailed = false ;
19- errorMessage = '' ;
20- roles : string [ ] = [ ] ;
21-
22- constructor ( private authService : AuthenticationService , private tokenStorage : AuthenticationTokenService ) { }
23-
24- ngOnInit ( ) : void {
25- if ( this . tokenStorage . getToken ( ) ) {
26- this . isLoggedIn = true ;
27- this . roles = this . tokenStorage . getUser ( ) . roles ;
20+ constructor (
21+ private formBuilder : FormBuilder ,
22+ private route : ActivatedRoute ,
23+ private router : Router ,
24+ private authenticationService : AuthenticationService
25+ ) {
26+ // redirect to home if already logged in
27+ if ( this . authenticationService . userValue ) {
28+ this . router . navigate ( [ '/' ] ) ;
2829 }
30+
31+ this . loginForm = this . formBuilder . group ( {
32+ username : [ '' , Validators . required ] ,
33+ password : [ '' , Validators . required ]
34+ } ) ;
2935 }
30- onSubmit ( ) : void {
31- const { username, password } = this . form ;
32- this . authService . login ( username , password ) . subscribe (
33- data => {
34- this . tokenStorage . saveToken ( data . accessToken ) ;
35- this . tokenStorage . saveUser ( data ) ;
36- this . isLoginFailed = false ;
37- this . isLoggedIn = true ;
38- this . roles = this . tokenStorage . getUser ( ) . roles ;
39- this . reloadPage ( ) ;
40- } ,
41- err => {
42- this . errorMessage = err . error . message ;
43- this . isLoginFailed = true ;
44- }
45- ) ;
36+
37+ ngOnInit ( ) {
38+ // get return url from route parameters or default to '/'
39+ this . returnUrl = this . route . snapshot . queryParams [ 'returnUrl' ] || '/' ;
4640 }
47- reloadPage ( ) : void {
48- window . location . reload ( ) ;
41+
42+ // convenience getter for easy access to form fields
43+ get f ( ) { return this . loginForm . controls ; }
44+
45+ onSubmit ( ) {
46+ this . submitted = true ;
47+
48+ // stop here if form is invalid
49+ if ( this . loginForm . invalid ) {
50+ return ;
51+ }
52+
53+ this . loading = true ;
54+ this . authenticationService . login ( this . f . username . value , this . f . password . value )
55+ . pipe ( first ( ) )
56+ . subscribe (
57+ data => {
58+ this . router . navigate ( [ this . returnUrl ] ) ;
59+ } ,
60+ error => {
61+ this . error = error ;
62+ this . loading = false ;
63+ } ) ;
4964 }
5065
5166}
0 commit comments