-
Notifications
You must be signed in to change notification settings - Fork 555
/
actions.js
317 lines (261 loc) · 9.17 KB
/
actions.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import { getEntity, read, swap, updateEntity } from '../../store/index';
import webApi from '../../core/web_api';
import { closeLock, logIn as coreLogIn, logInSuccess, validateAndSubmit } from '../../core/actions';
import * as l from '../../core/index';
import * as c from '../../field/index';
import { sanitize } from 'dompurify';
import {
databaseConnection,
databaseConnectionName,
databaseConnectionRequiresUsername,
databaseLogInWithEmail,
hasScreen,
setScreen,
shouldAutoLogin,
toggleTermsAcceptance as internalToggleTermsAcceptance,
additionalSignUpFields,
signUpHideUsernameField
} from './index';
import * as i18n from '../../i18n';
import { setCaptchaParams, showMissingCaptcha, swapCaptcha } from '../captcha';
export function logIn(id, needsMFA = false) {
const m = read(getEntity, 'lock', id);
const usernameField = databaseLogInWithEmail(m) ? 'email' : 'username';
const username = c.getFieldValue(m, usernameField);
const params = {
connection: databaseConnectionName(m),
username: username,
password: c.getFieldValue(m, 'password')
};
const fields = [usernameField, 'password'];
const isCaptchaValid = setCaptchaParams(m, params, false, fields);
if (!isCaptchaValid) {
return showMissingCaptcha(m, id);
}
const mfaCode = c.getFieldValue(m, 'mfa_code');
if (needsMFA) {
params['mfa_code'] = mfaCode;
fields.push('mfa_code');
}
coreLogIn(id, fields, params, (id, error, fields, next) => {
if (error.error === 'a0.mfa_required') {
return showLoginMFAActivity(id);
}
if (error) {
const wasInvalid = error && error.code === 'invalid_captcha';
return swapCaptcha(id, false, wasInvalid, next);
}
next();
});
}
function generateRandomUsername(length) {
let result = '';
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
export function signUp(id) {
const m = read(getEntity, 'lock', id);
const fields = ['email', 'password'];
// Skip the username validation if signUpHideUsernameField option is enabled.
// We will generate a random username to avoid name collusion before we make the signup API call.
if (databaseConnectionRequiresUsername(m) && !signUpHideUsernameField(m)) fields.push('username');
additionalSignUpFields(m).forEach(x => fields.push(x.get('name')));
validateAndSubmit(id, fields, m => {
const params = {
connection: databaseConnectionName(m),
email: c.getFieldValue(m, 'email'),
password: c.getFieldValue(m, 'password'),
autoLogin: shouldAutoLogin(m)
};
const isCaptchaValid = setCaptchaParams(m, params, false, fields);
if (!isCaptchaValid) {
return showMissingCaptcha(m, id);
}
if (databaseConnectionRequiresUsername(m)) {
if (signUpHideUsernameField(m)) {
const usernameValidation = databaseConnection(m).getIn(['validation', 'username']);
const range = usernameValidation ? usernameValidation.toJS() : { max: 15 };
params.username = generateRandomUsername(range.max);
} else {
params.username = c.getFieldValue(m, 'username');
}
}
if (!additionalSignUpFields(m).isEmpty()) {
params.user_metadata = {};
additionalSignUpFields(m).forEach(x => {
const storage = x.get('storage');
const fieldName = x.get('name');
const fieldValue = sanitize(c.getFieldValue(m, x.get('name')), { ALLOWED_TAGS: [] });
switch (storage) {
case 'root':
params[fieldName] = fieldValue;
break;
default:
if (!params.user_metadata) {
params.user_metadata = {};
}
params.user_metadata[fieldName] = fieldValue;
break;
}
});
}
const errorHandler = (error, popupHandler) => {
if (!!popupHandler) {
popupHandler._current_popup.kill();
}
const wasInvalidCaptcha = error && error.code === 'invalid_captcha';
swapCaptcha(id, false, wasInvalidCaptcha, () => {
setTimeout(() => signUpError(id, error), 250);
});
};
try {
// For now, always pass 'null' for the context as we don't need it yet.
// If we need it later, it'll save a breaking change in hooks already in use.
const context = null;
l.runHook(m, 'signingUp', context, () => {
webApi.signUp(id, params, (error, result, popupHandler, ...args) => {
if (error) {
errorHandler(error, popupHandler);
} else {
signUpSuccess(id, result, popupHandler, ...args);
}
});
});
} catch (e) {
errorHandler(e);
}
});
}
function signUpSuccess(id, result, popupHandler) {
const lock = read(getEntity, 'lock', id);
l.emitEvent(lock, 'signup success', result);
if (shouldAutoLogin(lock)) {
swap(updateEntity, 'lock', id, m => m.set('signedUp', true));
// TODO: check options, redirect is missing
const options = {
connection: databaseConnectionName(lock),
username: c.email(lock),
password: c.password(lock)
};
if (!!popupHandler) {
options.popupHandler = popupHandler;
}
return webApi.logIn(id, options, l.auth.params(lock).toJS(), (error, ...args) => {
if (error) {
setTimeout(() => autoLogInError(id, error), 250);
} else {
logInSuccess(id, ...args);
}
});
}
const autoclose = l.ui.autoclose(lock);
if (!autoclose) {
swap(updateEntity, 'lock', id, lock => l.setSubmitting(lock, false).set('signedUp', true));
} else {
closeLock(id, false);
}
}
export function signUpError(id, error) {
const m = read(getEntity, 'lock', id);
const invalidPasswordKeys = {
PasswordDictionaryError: 'password_dictionary_error',
PasswordNoUserInfoError: 'password_no_user_info_error',
PasswordStrengthError: 'password_strength_error'
};
l.emitEvent(m, 'signup error', error);
const errorKey =
(error.code === 'invalid_password' && invalidPasswordKeys[error.name]) || error.code;
let errorMessage =
i18n.html(m, ['error', 'signUp', errorKey]) ||
i18n.html(m, ['error', 'signUp', 'lock.fallback']);
if (error.code === 'hook_error') {
swap(updateEntity, 'lock', id, l.setSubmitting, false, error.description || errorMessage);
return;
}
if (errorKey === 'invalid_captcha') {
errorMessage = i18n.html(m, ['error', 'login', errorKey]);
return swapCaptcha(id, false, true, () => {
swap(updateEntity, 'lock', id, l.setSubmitting, false, errorMessage);
});
}
swap(updateEntity, 'lock', id, l.setSubmitting, false, errorMessage);
}
function autoLogInError(id, error) {
swap(updateEntity, 'lock', id, m => {
const errorMessage = l.loginErrorMessage(m, error);
if (hasScreen(m, 'login')) {
return l.setSubmitting(setScreen(m, 'login'), false, errorMessage);
} else {
return l.setSubmitting(m, false, errorMessage);
}
});
}
export function resetPassword(id) {
validateAndSubmit(id, ['email'], m => {
const params = {
connection: databaseConnectionName(m),
email: c.getFieldValue(m, 'email')
};
webApi.resetPassword(id, params, (error, ...args) => {
if (error) {
setTimeout(() => resetPasswordError(id, error), 250);
} else {
resetPasswordSuccess(id);
}
});
});
}
function resetPasswordSuccess(id) {
const m = read(getEntity, 'lock', id);
if (hasScreen(m, 'login')) {
swap(
updateEntity,
'lock',
id,
m => setScreen(l.setSubmitting(m, false), 'login', ['']) // array with one empty string tells the function to not clear any field
);
// TODO: should be handled by box
setTimeout(() => {
const successMessage = i18n.html(m, ['success', 'forgotPassword']);
swap(updateEntity, 'lock', id, l.setGlobalSuccess, successMessage);
}, 500);
} else {
if (l.ui.autoclose(m)) {
closeLock(id);
} else {
swap(updateEntity, 'lock', id, m => l.setSubmitting(m, false).set('passwordResetted', true));
}
}
}
function resetPasswordError(id, error) {
const m = read(getEntity, 'lock', id);
const errorMessage =
i18n.html(m, ['error', 'forgotPassword', error.code]) ||
i18n.html(m, ['error', 'forgotPassword', 'lock.fallback']);
swap(updateEntity, 'lock', id, l.setSubmitting, false, errorMessage);
}
export function showLoginActivity(id, fields = ['password']) {
swap(updateEntity, 'lock', id, setScreen, 'login', fields);
}
export function showSignUpActivity(id, fields = ['password']) {
swap(updateEntity, 'lock', id, setScreen, 'signUp', fields);
}
export function showResetPasswordActivity(id, fields = ['password']) {
swap(updateEntity, 'lock', id, setScreen, 'forgotPassword', fields);
}
export function cancelResetPassword(id) {
return showLoginActivity(id);
}
export function cancelMFALogin(id) {
return showLoginActivity(id);
}
export function toggleTermsAcceptance(id) {
swap(updateEntity, 'lock', id, internalToggleTermsAcceptance);
}
export function showLoginMFAActivity(id, fields = ['mfa_code']) {
swap(updateEntity, 'lock', id, setScreen, 'mfaLogin', fields);
}