55 Validators ,
66 FormsModule ,
77 ReactiveFormsModule ,
8+ ValidatorFn ,
9+ AbstractControl ,
10+ ValidationErrors ,
811} from "@angular/forms" ;
912import { ApiService } from "../services/api-service" ;
1013import { SessionStorage } from "../services/SessionStorage" ;
@@ -55,28 +58,24 @@ export class LoginComponent implements OnInit, AfterViewInit {
5558 private route : ActivatedRoute ,
5659 ) { }
5760 ngAfterViewInit ( ) : void {
58- let posRegister = this . router . url . indexOf ( "register" ) ;
59- // If the url part of the URL and not the parameter contains register, we are in register mode
60- this . isRegisterMode = posRegister >= 0 ;
6161 this . sub = this . route . paramMap . subscribe ( ( params ) => {
6262 this . toUrl = params . get ( "toUrl" ) || "" ;
6363 } ) ;
6464 }
6565
6666 ngOnInit ( ) : void {
67- this . form = this . _formBuilder . group ( {
68- username : [ "" , Validators . required ] ,
69- password : [ "" , Validators . required ] ,
70- } ) ;
67+ let posRegister = this . router . url . indexOf ( "register" ) ;
68+ // If the url part of the URL and not the parameter contains register, we are in register mode
69+ this . isRegisterMode = posRegister >= 0 ;
70+ this . form = this . _formBuilder . group ( {
71+ username : [ "" , this . usernamePasswordRequired ] ,
72+ password : [ "" , this . usernamePasswordRequired ] ,
73+ } ) ;
7174 }
72- private login ( username : string , password : string ) {
75+ private login ( username : string , password : string , toUrl : string ) {
7376 this . api . getUserLogin ( username , password ) . subscribe ( ( token ) => {
7477 new SessionStorage ( ) . setAuthToken ( token ) ;
75- var u = new URLSearchParams ( this . router . url . replace ( / ^ [ ^ ? ] * \? / , "" ) ) ;
76- var toUrl = u . get ( "toUrl" ) ;
77- if ( u && toUrl ) this . toUrl = toUrl ;
78- else this . toUrl = "" ;
79- this . router . navigate ( [ this . toUrl ] , {
78+ this . router . navigate ( [ toUrl ] , {
8079 queryParams : { tokenWasExpired : true } ,
8180 } ) ;
8281 } ) ;
@@ -86,11 +85,26 @@ export class LoginComponent implements OnInit, AfterViewInit {
8685 let password = this . form . get ( "password" ) ! . value ;
8786 if ( this . isRegisterMode ) {
8887 let noAuthentication = ( event . submitter as HTMLButtonElement ) . value == 'noAuthentication'
88+ if ( ! noAuthentication && ! username )
89+ alert ( "Please enter a username" )
90+ else if ( ! noAuthentication && ! password )
91+ alert ( "Please enter a password" )
92+ else
8993 this . api . postUserRegister ( username , password , noAuthentication ) . subscribe ( ( ) => {
9094 if ( ! noAuthentication )
91- this . login ( username , password ) ;
95+ this . login ( username , password , '/' ) ;
9296 else this . router . navigate ( [ '/' ] , { } ) ;
9397 } ) ;
94- } else this . login ( username , password ) ;
98+ } else this . login ( username , password , '/' ) ;
9599 }
100+ usernamePasswordRequired ( ) : ValidatorFn {
101+ return ( control :AbstractControl ) : ValidationErrors | null => {
102+ if ( ! this . isRegisterMode )
103+ return Validators . required ( control )
104+ else {
105+ return null ;
106+ }
107+ }
108+ }
109+
96110}
0 commit comments