src/app/login/login.component.ts
This component is responsible for the login screen. It gather username and password and tries to login the user via the AuthenticationService.
| selector | app-login |
| styleUrls | ./login.component.scss |
| templateUrl | ./login.component.html |
Properties |
Methods |
constructor(route: ActivatedRoute, router: Router, authService: AuthenticationService, fb: FormBuilder)
|
|||||||||||||||
|
Defined in src/app/login/login.component.ts:51
|
|||||||||||||||
|
Parameters :
|
| submitForm |
submitForm()
|
|
Defined in src/app/login/login.component.ts:29
|
|
Gather username and password from form and try login via AuthenticationService. If successfull redirect to root url or to the redirectUrl the user originally wanted to access. If login fails, set this.invalidCredentials so that gui shows error.
Returns :
void
|
| invalidCredentials |
Default value : false
|
|
Defined in src/app/login/login.component.ts:21
|
| isLoading |
Default value : false
|
|
Defined in src/app/login/login.component.ts:20
|
| returnUrl |
Type : string
|
|
Defined in src/app/login/login.component.ts:19
|
| unknownError |
Default value : false
|
|
Defined in src/app/login/login.component.ts:22
|
| validateForm |
Type : FormGroup
|
|
Defined in src/app/login/login.component.ts:18
|
import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {ActivatedRoute, Router} from '@angular/router';
import {first} from 'rxjs/operators';
import {AuthenticationService} from '../auth/authentication.service';
import {HttpErrorResponse} from '@angular/common/http';
/**
* This component is responsible for the login screen. It gather username and password
* and tries to login the user via the AuthenticationService.
*/
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
validateForm!: FormGroup;
returnUrl: string;
isLoading = false;
invalidCredentials = false;
unknownError = false;
/**
* Gather username and password from form and try login via AuthenticationService.
* If successfull redirect to root url or to the redirectUrl the user originally wanted to access.
* If login fails, set this.invalidCredentials so that gui shows error.
*/
submitForm(): void {
Object.keys(this.validateForm.controls).forEach((controlKey) => {
this.validateForm.controls[controlKey].markAsDirty();
this.validateForm.controls[controlKey].updateValueAndValidity();
});
this.isLoading = true;
this.authService
.login(this.validateForm.controls.userName.value, this.validateForm.controls.password.value)
.pipe(first())
.subscribe(
() => {
this.validateForm.controls.password.reset();
this.isLoading = false;
this.router.navigate([this.returnUrl]);
},
(error: HttpErrorResponse) => {
this.validateForm.controls.password.reset();
this.isLoading = false;
this.invalidCredentials = error.status === 401;
this.unknownError = error.status === 0;
}
);
}
constructor(private route: ActivatedRoute, private router: Router, private authService: AuthenticationService, private fb: FormBuilder) {}
ngOnInit(): void {
this.validateForm = this.fb.group({
userName: [null, [Validators.required]],
password: [null, [Validators.required]]
});
this.returnUrl = this.route.snapshot.queryParams.returnUrl || '/';
}
}
<div class="canvas-background">
<div class="form-container">
<div class="logo-wrapper">
<div class="logo-container">
<div class="logo-image">
<img src="../../assets/Gropius.png" alt="CCIMS-Logo" height="80" />
</div>
</div>
<div class="headline"><h2>Login</h2></div>
</div>
<form nz-form [formGroup]="validateForm" class="login-form" (ngSubmit)="submitForm()">
<nz-form-item>
<nz-form-control nzErrorTip="Please enter your Username!">
<nz-input-group nzPrefixIcon="user">
<input type="text" nz-input formControlName="userName" placeholder="Username" />
</nz-input-group>
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-control nzErrorTip="Please enter your Password!">
<nz-input-group nzPrefixIcon="lock">
<input type="password" nz-input formControlName="password" placeholder="Password" />
</nz-input-group>
</nz-form-control>
</nz-form-item>
<nz-alert
*ngIf="invalidCredentials"
class="error-message"
nzType="error"
nzMessage="Username and/or Password invalid"
nzShowIcon
nzCloseable
></nz-alert>
<nz-alert *ngIf="unknownError" class="error-message" nzType="error" nzMessage="Unknown error" nzShowIcon nzCloseable></nz-alert>
<button
nz-button
class="login-form-button login-form-margin"
[nzType]="'primary'"
[disabled]="!validateForm.valid"
[nzLoading]="isLoading"
>
Log in
</button>
<span id="register-link">Or <a routerLink="/register">register!</a></span>
</form>
</div>
</div>
./login.component.scss
.login-form {
max-width: 300px;
margin: auto;
}
.login-form-margin {
margin-bottom: 16px;
}
.login-form-forgot {
float: right;
}
.login-form-button {
width: 100%;
}
.error-message {
margin-bottom: 24px;
}
#register-link {
font-size: larger;
}