-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathlogin-or-register-oidc.js
156 lines (125 loc) · 4.23 KB
/
login-or-register-oidc.js
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
import { action } from '@ember/object';
import { service } from '@ember/service';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import get from 'lodash/get';
import isEmpty from 'lodash/isEmpty';
import isEmailValid from '../../utils/email-validator';
const ERROR_INPUT_MESSAGE_MAP = {
termsOfServiceNotSelected: 'pages.login-or-register-oidc.error.error-message',
invalidEmail: 'pages.login-or-register-oidc.error.invalid-email',
};
export default class LoginOrRegisterOidcComponent extends Component {
@service intl;
@service session;
@service currentDomain;
@service oidcIdentityProviders;
@service store;
@service url;
@service errorMessages;
@tracked isTermsOfServiceValidated = false;
@tracked loginErrorMessage = null;
@tracked registerErrorMessage = null;
@tracked email = '';
@tracked password = '';
@tracked emailValidationStatus = 'default';
@tracked emailValidationMessage = null;
@tracked isLoginLoading = false;
@tracked isRegisterLoading = false;
get identityProviderOrganizationName() {
return this.oidcIdentityProviders[this.args.identityProviderSlug]?.organizationName;
}
get currentLanguage() {
return this.intl.primaryLocale;
}
get cguUrl() {
return this.url.cguUrl;
}
get dataProtectionPolicyUrl() {
return this.url.dataProtectionPolicyUrl;
}
get userClaimsErrorMessage() {
const { userClaims } = this.args;
if (!userClaims) {
return this.intl.t(`pages.login-or-register-oidc.register-form.information.error`);
} else {
return null;
}
}
get userClaimsToDisplay() {
const { userClaims } = this.args;
const result = [];
if (userClaims) {
const { firstName, lastName, ...rest } = userClaims;
result.push(`${this.intl.t(`pages.login-or-register-oidc.register-form.information.firstName`)} ${firstName}`);
result.push(`${this.intl.t(`pages.login-or-register-oidc.register-form.information.lastName`)} ${lastName}`);
Object.entries(rest).map(([key, value]) => {
let label = `${this.intl.t(`pages.login-or-register-oidc.register-form.information.${key}`)}`;
const translation = `${this.intl.t(`pages.login-or-register-oidc.register-form.information.${key}`)}`;
if (translation.includes('Missing translation')) {
label = `${key} :`;
}
return result.push(`${label} ${value}`);
});
}
return result;
}
@action
async login(event) {
event.preventDefault();
this.loginErrorMessage = null;
if (!this.isFormValid) return;
this.isLoginLoading = true;
try {
await this.args.onLogin({ enteredEmail: this.email, enteredPassword: this.password });
} catch (responseError) {
this.loginErrorMessage = this.errorMessages.getErrorMessage(responseError);
} finally {
this.isLoginLoading = false;
}
}
@action
async register() {
if (!this.isTermsOfServiceValidated) {
this.registerErrorMessage = this.intl.t(ERROR_INPUT_MESSAGE_MAP['termsOfServiceNotSelected']);
return;
}
this.isRegisterLoading = true;
this.registerErrorMessage = null;
try {
await this.session.authenticate('authenticator:oidc', {
authenticationKey: this.args.authenticationKey,
identityProviderSlug: this.args.identityProviderSlug,
hostSlug: 'users',
});
} catch (responseError) {
const error = get(responseError, 'errors[0]');
this.registerErrorMessage = this.errorMessages.getErrorMessage(error);
} finally {
this.isRegisterLoading = false;
}
}
@action
validateEmail(event) {
this.email = event.target.value;
this.email = this.email.trim();
const isInvalidInput = !isEmailValid(this.email);
this.emailValidationMessage = null;
this.emailValidationStatus = 'default';
if (isInvalidInput) {
this.emailValidationStatus = 'error';
this.emailValidationMessage = this.intl.t(ERROR_INPUT_MESSAGE_MAP['invalidEmail']);
}
}
@action
setPassword(event) {
this.password = event.target.value;
}
get isFormValid() {
return isEmailValid(this.email) && !isEmpty(this.password);
}
@action
onChange(event) {
this.isTermsOfServiceValidated = !!event.target.checked;
}
}