-
Notifications
You must be signed in to change notification settings - Fork 219
/
__auth._login.component.ts
103 lines (89 loc) · 2.87 KB
/
__auth._login.component.ts
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
99
100
101
102
103
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
<% if (props.ui === 'ionic') { -%>
import { LoadingController, Platform } from '@ionic/angular';
import { map } from 'rxjs/operators';
import { forkJoin, from } from 'rxjs';
<% } -%>
import { finalize } from 'rxjs/operators';
import { environment } from '@env/environment';
import { Logger, I18nService, untilDestroyed } from '@app/core';
import { AuthenticationService } from './authentication.service';
const log = new Logger('Login');
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit, OnDestroy {
version: string | null = environment.version;
error: string | undefined;
loginForm!: FormGroup;
isLoading = false;
constructor(private router: Router,
private route: ActivatedRoute,
private formBuilder: FormBuilder,
<% if (props.ui === 'ionic') { -%>
private platform: Platform,
private loadingController: LoadingController,
<% } -%>
private i18nService: I18nService,
private authenticationService: AuthenticationService) {
this.createForm();
}
ngOnInit() { }
ngOnDestroy() { }
<% if (props.ui === 'ionic') { -%>
async login() {
<% } else { -%>
login() {
<% } -%>
this.isLoading = true;
const login$ = this.authenticationService.login(this.loginForm.value);
<% if (props.ui === 'ionic') { -%>
const loadingOverlay = await this.loadingController.create({});
const loading$ = from(loadingOverlay.present());
forkJoin([login$, loading$]).pipe(
map(([credentials, ...rest]) => credentials),
<% } else { -%>
login$.pipe(
<% } -%>
finalize(() => {
this.loginForm.markAsPristine();
this.isLoading = false;
<% if (props.ui === 'ionic') { -%>
loadingOverlay.dismiss();
<% } -%>
}),
untilDestroyed(this)
).subscribe(credentials => {
log.debug(`${credentials.username} successfully logged in`);
this.router.navigate([ this.route.snapshot.queryParams.redirect || '/'], { replaceUrl: true });
}, error => {
log.debug(`Login error: ${error}`);
this.error = error;
});
}
setLanguage(language: string) {
this.i18nService.language = language;
}
get currentLanguage(): string {
return this.i18nService.language;
}
get languages(): string[] {
return this.i18nService.supportedLanguages;
}
<% if (props.ui === 'ionic') { -%>
get isWeb(): boolean {
return !this.platform.is('cordova');
}
<% } -%>
private createForm() {
this.loginForm = this.formBuilder.group({
username: ['', Validators.required],
password: ['', Validators.required],
remember: true
});
}
}