-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
login.component.ts
81 lines (75 loc) · 2.47 KB
/
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
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Inject, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { ROPCService } from '../../ropc.service';
import { Router } from '@angular/router';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
import { HttpErrorResponse } from '@angular/common/http';
import { OAuthService } from '@xmlking/angular-oauth2-oidc-all';
import { Store } from '@ngxs/store';
import { ChangeAuthMode, AuthMode } from '../../auth.actions';
/** @dynamic */
@Component({
selector: 'ngx-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss'],
})
export class LoginComponent {
public infoMsg: String;
public errorMsg: String;
inputType = 'password';
visible = false;
loginForm: FormGroup;
constructor(
fb: FormBuilder,
private store: Store,
private cd: ChangeDetectorRef,
private oauthService: OAuthService,
private ropcService: ROPCService,
private router: Router,
@Inject(MAT_DIALOG_DATA) public data: { infoMsg: string },
public dialogRef: MatDialogRef<LoginComponent>,
) {
if (data) {
this.infoMsg = data.infoMsg;
}
this.loginForm = fb.group({
username: ['', Validators.compose([Validators.required, Validators.minLength(4)])],
password: ['', Validators.compose([Validators.required, Validators.minLength(4)])],
rememberMe: false,
});
}
toggleInputType() {
if (this.visible) {
this.inputType = 'password';
this.visible = false;
this.cd.markForCheck();
} else {
this.inputType = 'text';
this.visible = true;
this.cd.markForCheck();
}
}
initSSO() {
this.store.dispatch(new ChangeAuthMode(AuthMode.ImplicitFLow)).subscribe(() => {
this.oauthService.initImplicitFlow();
console.log('initSSO');
});
}
onSubmit(values) {
if (this.loginForm.invalid) {
return;
}
this.store.dispatch(new ChangeAuthMode(AuthMode.PasswordFlow)).subscribe(async () => {
try {
const profile = await this.ropcService.login(values.username, values.password);
this.dialogRef.close(profile);
} catch (error /*: HttpErrorResponse*/) {
if (error.error && error.error.error_description !== undefined) {
this.errorMsg = error.error.error_description; // 'The user credentials is incorrect';
} else {
this.errorMsg = 'Login Failed';
}
}
});
}
}