forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog-in-password.component.ts
237 lines (209 loc) · 6.9 KB
/
log-in-password.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { map } from 'rxjs/operators';
import { Component, Inject, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { select, Store } from '@ngrx/store';
import { firstValueFrom, Observable } from 'rxjs';
import {
AuthenticateAction,
ResetAuthenticationMessagesAction
} from '../../../../core/auth/auth.actions';
import { getAuthenticationError, getAuthenticationInfo } from '../../../../core/auth/selectors';
import { isEmpty, isNotEmpty } from '../../../empty.util';
import { fadeOut } from '../../../animations/fade';
import { AuthMethodType } from '../../../../core/auth/models/auth.method-type';
import { renderAuthMethodFor } from '../log-in.methods-decorator';
import { AuthMethod } from '../../../../core/auth/models/auth.method';
import { AuthService } from '../../../../core/auth/auth.service';
import { CoreState } from '../../../../core/core-state.model';
import { ActivatedRoute , Router} from '@angular/router';
import { getBaseUrl } from '../../../clarin-shared-util';
import { ConfigurationProperty } from '../../../../core/shared/configuration-property.model';
import { ConfigurationDataService } from '../../../../core/data/configuration-data.service';
/**
* /users/sign-in
* @class LogInPasswordComponent
*/
@Component({
selector: 'ds-log-in-password',
templateUrl: './log-in-password.component.html',
styleUrls: ['./log-in-password.component.scss'],
animations: [fadeOut],
})
@renderAuthMethodFor(AuthMethodType.Password)
export class LogInPasswordComponent implements OnInit {
/**
* The authentication method data.
* @type {AuthMethod}
*/
public authMethod: AuthMethod;
/**
* The error if authentication fails.
* @type {Observable<string>}
*/
public error: Observable<string>;
/**
* Has authentication error.
* @type {boolean}
*/
public hasError = false;
/**
* The authentication info message.
* @type {Observable<string>}
*/
public message: Observable<string>;
/**
* Has authentication message.
* @type {boolean}
*/
public hasMessage = false;
/**
* The authentication form.
* @type {FormGroup}
*/
public form: FormGroup;
/**
* The page from where the local login was initiated.
*/
public redirectUrl = '';
/**
* `dspace.ui.url` property fetched from the server.
*/
public baseUrl = '';
/**
* @constructor
* @param {AuthMethod} injectedAuthMethodModel
* @param {boolean} isStandalonePage
* @param {AuthService} authService
* @param {HardRedirectService} hardRedirectService
* @param {FormBuilder} formBuilder
* @param {Store<State>} store
* @param route
* @param router
* @param configurationService
*/
constructor(
@Inject('authMethodProvider') public injectedAuthMethodModel: AuthMethod,
@Inject('isStandalonePage') public isStandalonePage: boolean,
private authService: AuthService,
private formBuilder: FormBuilder,
private store: Store<CoreState>,
private route: ActivatedRoute,
protected router: Router,
protected configurationService: ConfigurationDataService,
) {
this.authMethod = injectedAuthMethodModel;
}
/**
* Lifecycle hook that is called after data-bound properties of a directive are initialized.
* @method ngOnInit
*/
public async ngOnInit() {
this.redirectUrl = '';
// set formGroup
this.form = this.formBuilder.group({
email: ['', Validators.required],
password: ['', Validators.required]
});
// set error
this.error = this.store.pipe(select(
getAuthenticationError),
map((error) => {
this.hasError = (isNotEmpty(error));
return error;
})
);
// set error
this.message = this.store.pipe(
select(getAuthenticationInfo),
map((message) => {
this.hasMessage = (isNotEmpty(message));
return message;
})
);
// Load `dspace.ui.url` into `baseUrl` property.
await this.assignBaseUrl();
this.toggleDiscojuiceLogin();
void this.setUpRedirectUrl();
}
/**
* Set up redirect URL. It could be loaded from the `authorizationService.getRedirectUrl()` or from the url.
*/
public async setUpRedirectUrl() {
const fetchedRedirectUrl = await firstValueFrom(this.authService.getRedirectUrl());
if (isNotEmpty(fetchedRedirectUrl)) {
// Bring over the item ID as a query parameter
const queryParams = { redirectUrl: fetchedRedirectUrl };
// Redirect to login with `redirectUrl` param because the redirectionUrl is lost from the store after click on
// `local` login.
void this.router.navigate(['login'], { queryParams: queryParams });
}
// Store the `redirectUrl` value from the url and then remove that value from url.
// Overwrite `this.redirectUrl` only if it's not stored in the authService `redirectUrl` property.
if (isEmpty(this.redirectUrl)) {
this.redirectUrl = this.route.snapshot.queryParams?.redirectUrl;
}
}
private toggleDiscojuiceLogin() {
if (isEmpty(this.route.snapshot.queryParams?.redirectUrl)) {
this.popUpDiscoJuiceLogin();
}
}
/**
* Reset error or message.
*/
public resetErrorOrMessage() {
if (this.hasError || this.hasMessage) {
this.store.dispatch(new ResetAuthenticationMessagesAction());
this.hasError = false;
this.hasMessage = false;
}
}
/**
* Submit the authentication form.
* @method submit
*/
public submit() {
this.resetErrorOrMessage();
// get email and password values
const email: string = this.form.get('email').value;
const password: string = this.form.get('password').value;
// trim values
email.trim();
password.trim();
if (!this.isStandalonePage || isNotEmpty(this.redirectUrl)) {
// Create a URLSearchParams object
const urlParams = new URLSearchParams(this.redirectUrl.split('?')[1]);
// Get the value of the 'redirectUrl' parameter
let redirectUrl = urlParams.get('redirectUrl');
if (isEmpty(redirectUrl)) {
redirectUrl = this.redirectUrl;
}
this.authService.setRedirectUrl(redirectUrl.replace(this.baseUrl, ''));
} else {
this.authService.setRedirectUrlIfNotSet('/');
}
// dispatch AuthenticationAction
this.store.dispatch(new AuthenticateAction(email, password));
// clear form
this.form.reset();
}
/**
* Load the `dspace.ui.url` into `baseUrl` property.
*/
async assignBaseUrl() {
this.baseUrl = await getBaseUrl(this.configurationService)
.then((baseUrlResponse: ConfigurationProperty) => {
return baseUrlResponse?.values?.[0];
});
}
/**
* Show DiscoJuice login modal using javascript functions. The timeout must be set because of angular component
* lifecycle. Discojuice won't be showed up without timeout.
* @private
*/
private popUpDiscoJuiceLogin() {
setTimeout(() => {
document?.getElementById('clarin-signon-discojuice')?.click();
}, 250);
}
}