Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { Component, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
import { Validators, FormGroup, FormBuilder } from '@angular/forms';
import { tap, catchError, finalize } from 'rxjs/operators';
import { AuthService } from '../../service/auth.service';
import { NotificationService } from '../notification.service';
import { environment } from '../../environments/environment';
import { LangService } from '../../service/app-lang.service';
import { of } from 'rxjs/internal/observable/of';
import { HeaderService } from '../../service/header.service';
@Component({
templateUrl: 'login.component.html',
styleUrls: ['login.component.scss'],
})
export class LoginComponent implements OnInit {
lang: any = this.langService.getLang();
loginForm: FormGroup;
loading: boolean = false;
showForm: boolean = false;
environment: any;
applicationName: string = '';
loginMessage: string = '';
constructor(
private langService: LangService,
private http: HttpClient,
private router: Router,
private headerService: HeaderService,
public authService: AuthService,
private notify: NotificationService,
public dialog: MatDialog,
private formBuilder: FormBuilder
) { }
ngOnInit(): void {
this.headerService.hideSideBar = true;
this.loginForm = this.formBuilder.group({
login: [null, Validators.required],
password: [null, Validators.required]
});
this.environment = environment;
if (this.authService.isAuth()) {
this.router.navigate(['/home']);
} else {
this.getLoginInformations();
}
}
onSubmit() {
this.loading = true;
this.http.post(
'../rest/authenticate',
{
'login': this.loginForm.get('login').value,
'password': this.loginForm.get('password').value
},
{
observe: 'response'
}
).pipe(
tap((data: any) => {
this.authService.saveTokens(data.headers.get('Token'), data.headers.get('Refresh-Token'));
this.authService.setUser({});
this.router.navigate(['/home']);
}),
catchError((err: any) => {
this.loading = false;
if (err.status === 401) {
this.notify.error(this.lang.wrongLoginPassword);
} else {
this.notify.handleSoftErrors(err);
}
return of(false);
})
).subscribe();
}
getLoginInformations() {
this.http.get(
'../rest/authenticationInformations').pipe(
tap((data: any) => {
this.applicationName = data.applicationName;
this.loginMessage = data.loginMessage;
}),
finalize(() => this.showForm = true),
catchError((err: any) => {
this.notify.handleSoftErrors(err);
return of(false);
})
).subscribe();
}
}