/
login-form.component.ts
198 lines (166 loc) · 6.41 KB
/
login-form.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { AfterViewInit, Component, EventEmitter, Inject, Input, OnInit, Output } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { ApiResponseData, ApiResponseError, KnoraApiConnection, LoginResponse } from '@dasch-swiss/dsp-js';
import { DspApiConnectionToken } from '../../declarations/dsp-api-tokens';
import { ErrorHandlerService } from '../../services/error-handler.service';
import { AuthenticationService } from '../../services/authentication.service';
import { ComponentCommunicationEventService, EmitEvent, Events } from '../../services/component-communication-event.service';
import { DatadogRumService } from '../../services/datadog-rum.service';
import { Session, SessionService } from '../../services/session.service';
@Component({
selector: 'app-login-form',
templateUrl: './login-form.component.html',
styleUrls: ['./login-form.component.scss']
})
export class LoginFormComponent implements OnInit, AfterViewInit {
/**
* set whether or not you want icons to display in the input fields
*
* @param icons
*/
@Input() icons?: boolean;
/**
* emits true when the login process was successful and false in case of error
*
* @param loginSuccess
*
*/
@Output() loginSuccess: EventEmitter<boolean> = new EventEmitter<boolean>();
/**
* emits true when the logout process was successful and false in case of error
*
* @param logoutSuccess
*
*/
@Output() logoutSuccess: EventEmitter<boolean> = new EventEmitter<boolean>();
// @ViewChild('username') usernameInput: ElementRef;
// is there already a valid session?
session: Session;
// form
form: FormGroup;
// show progress indicator
loading = false;
// url history
returnUrl: string;
// in case of an error
isError: boolean;
// specific error messages
loginFailed = false;
loginErrorServer = false;
// labels for the login form
// todo: should be handled by translation service (i18n)
formLabel = {
title: 'Login here',
name: 'Username',
pw: 'Password',
submit: 'Login',
retry: 'Retry',
logout: 'LOGOUT',
remember: 'Remember me',
forgotPassword: 'Forgot password?',
error: {
failed: 'Password or username is wrong',
server: 'An error has occurred when connecting to the server. Try again later or contact the DaSCH support.'
}
};
// error definitions for the following form fields
formErrors = {
username: '',
password: ''
};
// error messages for the form fields defined in formErrors
validationMessages = {
username: {
required: 'user name is required.'
},
password: {
required: 'password is required'
}
};
constructor(
@Inject(DspApiConnectionToken) private _dspApiConnection: KnoraApiConnection,
private _auth: AuthenticationService,
private _componentCommsService: ComponentCommunicationEventService,
private _datadogRumService: DatadogRumService,
private _errorHandler: ErrorHandlerService,
private _fb: FormBuilder,
private _session: SessionService,
private _route: ActivatedRoute,
private _router: Router,
) {
this.returnUrl = this._route.snapshot.queryParams['returnUrl'];
}
ngOnInit() {
// if session is valid (a user is logged-in) show a message, otherwise build the form
this._session.isSessionValid().subscribe(
result => {
// returns a result if session is still valid
if (result) {
this.session = JSON.parse(localStorage.getItem('session'));
} else {
// session is invalid, build the login form
this.buildLoginForm();
}
}
);
}
ngAfterViewInit() {
if (this.session) {
// this.usernameInput.nativeElement.focus();
}
}
buildLoginForm(): void {
this.form = this._fb.group({
username: ['', Validators.required],
password: ['', Validators.required]
});
}
/**
*
* login and set session
*/
login() {
this.loading = true;
this.isError = false;
// grab values from form
const identifier = this.form.get('username').value;
const password = this.form.get('password').value;
const identifierType: 'iri' | 'email' | 'username' = (identifier.indexOf('@') > -1 ? 'email' : 'username');
this._dspApiConnection.v2.auth.login(identifierType, identifier, password).subscribe(
(response: ApiResponseData<LoginResponse>) => {
this._session.setSession(response.body.token, identifier, identifierType).subscribe(
() => {
this.loginSuccess.emit(true);
this.session = this._session.getSession();
this._componentCommsService.emit(new EmitEvent(Events.loginSuccess, true));
this.returnUrl = this._route.snapshot.queryParams['returnUrl'];
if (this.returnUrl) {
this._router.navigate([this.returnUrl]);
} else {
window.location.reload();
}
this._datadogRumService.setActiveUser(identifier, identifierType);
this.loading = false;
}
);
},
(error: ApiResponseError) => {
// error handling
this.loginFailed = (error.status === 401 || error.status === 404);
this.loginErrorServer = (error.status === 0 || error.status >= 500 && error.status < 600);
if (this.loginErrorServer) {
this._errorHandler.showMessage(error);
}
this.isError = true;
this.loading = false;
// log error to Rollbar (done automatically by simply throwing a new Error)
throw new Error('login failed');
}
);
}
logout() {
// bring back the logout method and use it in the parent (somehow)
this._auth.logout();
}
}