-
Notifications
You must be signed in to change notification settings - Fork 19
/
login-fire-form.html
619 lines (566 loc) · 18.3 KB
/
login-fire-form.html
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
<!--
@license
Copyright (c) 2016 Convoo All Rights Reserved.
Use of this source code is governed by a MIT license that can be found in the
LICENSE file or at https://github.com/convoo/login-fire/blob/master/LICENSE.
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="./localize-behavior.html">
<link rel="import" href="./login-fire-form-behavior.html">
<link rel="import" href="../paper-button/paper-button.html">
<link rel="import" href="../paper-icon-button/paper-icon-button.html">
<link rel="import" href="../paper-input/paper-input.html">
<link rel="import" href="./icons.html">
<link rel="import" href="./login-fire-styles.html">
<link rel="import" href="../paper-progress/paper-progress.html">
<!--
A pre-built login form to authenticate a user with email and password using the
[Firebase Auth API](https://firebase.google.com/docs/auth/).
Example:
```
<firebase-app
name="myApp"
api-key="API_KEY"
auth-domain="AUTH_DOMAIN"
database-url="DATABASE_URL">
</firebase-app>
<login-fire-form app-name="myApp"></login-fire-form>
```
### Styling
Style the buttons with CSS as you would a normal DOM element.
The following custom properties and mixins are available for styling:
Custom property | Description | Default
--------------- | ----------- | -------
`--login-fire-btn-signin` | Mixin applied to the signin button | `{}`
`--login-fire-btn-signout` | Mixin applied to the signin button | `{}`
`--login-fire-btn-signup` | Mixin applied to the signup button | `{}`
`--login-fire-error-msg-color` | The color for the error message | `--error-color`
`--login-fire-form` | Mixin applied on the entire element | `{}`
`--login-fire-form-toggle` | Mixin applied on the div that contains the text asking if the user already has an account | `{}`
`--login-fire-info-msg-color` | The color for the info message | `gray`
`--login-fire-reset-password-color` | The color of reset password text | `gray`
`--login-fire-reset-password-hover-color` | The color of reset password text when hovered | `gray`
`--login-fire-progress-active-color` | The color that is applied to the paper-progress bar as active color | `--google-green-500`
`--login-fire-progress-container-color` | The color that is applied to the paper-progress-container | `--google-grey-300`
@customElement
@polymer
@demo demo/login-fire-form.html
-->
<dom-module id="login-fire-form">
<template>
<style include="login-fire-styles">
:host {
display: block;
@apply --layout-vertical;
width: 210px;
@apply --login-fire-form;
}
:host([wide]) {
width: 100%;
}
paper-button.btn--signup {
width: 100%;
margin-top: 10px;
@apply --login-fire-btn-signup;
}
paper-button.btn--signin {
width: 100%;
@apply --login-fire-btn-signin;
}
.btn {
color: gray;
}
.link--reset-pw {
width: fit-content;
color: var(--login-fire-reset-password-color, gray);
text-decoration: none;
text-transform: none;
font-size: 12px;
font-style: italic;
font-weight: lighter;
margin: 0;
padding-left: 0;
}
.link--reset-pw:hover {
color: var(--login-fire-reset-password-hover-color, gray);
text-decoration: underline;
}
.msg {
text-align: center;
font-size: 12px;
font-weight: 500;
line-height: 24px;
margin: 0;
padding: 0;
}
.msg--error {
color: var(--login-fire-error-msg-color, var(--error-color));
}
.msg--info {
color: var(--login-fire-info-msg-color, gray);
}
.signup-toggle {
font-size: 12px;
color: grey;
text-align: center;
@apply --login-fire-form-toggle;
}
.signup-toggle paper-button {
text-decoration: underline;
text-transform: none;
color: blue;
cursor: pointer;
margin: 0;
padding: 0;
min-width: 0;
}
paper-icon-button {
color: grey;
}
</style>
<div class="msg msg--error" hidden$="[[!_lastError]]">
<span>[[_lastError]]</span>
</div>
<div class="msg msg--info" hidden$="[[!_lastMessage]]">
<span>[[_lastMessage]]</span>
</div>
<paper-input id="emailInput"
type="email"
label="[[localize('lf-email')]]"
value="{{email}}"
pattern="[[emailRegex]]"
error-message="[[localize('lf-email-invalid')]]"
on-keydown="_submitOnEnter"
required>
</paper-input>
<template is="dom-if" if="[[!showResetPassword]]">
<paper-input id="passwordInput"
type="password"
label="[[localize('lf-password')]]"
value="{{password}}"
pattern="[[passwordRegex]]"
error-message="[[localize('lf-password-invalid')]]"
on-keydown="_submitOnEnter"
required>
<paper-icon-button
id="passwordIconButton"
slot="suffix"
suffix
on-down="_revealPW"
on-up="_hidePW"
icon="login-fire-icons:visibility"
title="[[localize('lf-reveal-password')]]"
hidden$="[[noToggleablePassword]]">
</paper-icon-button>
</paper-input>
<template is="dom-if" if="[[_showSignUp]]">
<paper-input id="repeatPasswordInput"
type="password"
label="[[localize('lf-reenter-password')]]"
value="{{_passwordAgain}}"
pattern="[[passwordRegex]]"
error-message="[[localize('lf-passwords-dont-match')]]"
on-keydown="_submitOnEnter"
hidden$="[[!reenterPassword]]"
required>
</paper-input>
<paper-button raised$="[[!flat]]" type="submit" on-click="confirm" class="btn--signup" disabled$="[[inProgress]]">
<span>[[localize('lf-signup-button')]]</span>
<paper-progress class="btn-progress" hidden$="[[!inProgress]]" disabled$="[[!inProgress]]" indeterminate></paper-progress>
</paper-button>
<div class="signup-toggle">
<span>[[localize('lf-already-have-account')]]</span>
<paper-button on-click="_toggleShowSignUp" tabindex="0"
title="[[localize('lf-already-have-account')]] [[localize('lf-already-have-account-action')]]" noink>
[[localize('lf-already-have-account-action')]]
</paper-button>
</div>
</template>
<template is="dom-if" if="[[!_showSignUp]]">
<paper-button class="link--reset-pw"
on-click="toggleShowResetPassword" tabindex="0" noink>
<span>[[localize('lf-resetpw-link')]]</span>
</paper-button>
<paper-button raised$="[[!flat]]" type="submit" on-click="confirm" class="btn--signin" disabled$="[[inProgress]]">
<span>[[localize('lf-signin-button')]]</span>
<paper-progress class="btn-progress" hidden$="[[!inProgress]]" disabled$="[[!inProgress]]" indeterminate></paper-progress>
</paper-button>
<template is="dom-if" if="[[!noSignUp]]">
<div class="signup-toggle">
<span>[[localize('lf-no-account')]]</span>
<paper-button on-click="_toggleShowSignUp" tabindex="0"
title="[[localize('lf-no-account')]] [[localize('lf-no-account-action')]]" noink>
[[localize('lf-no-account-action')]]
</paper-button>
</div>
</template>
</template>
</template>
<template is="dom-if" if="[[showResetPassword]]">
<paper-button class="link--reset-pw" on-click="toggleShowResetPassword"
tabindex="0" noink>[[localize('lf-resetpw-cancel-link')]]
</paper-button>
<paper-button raised$="[[!flat]]" type="submit" on-click="confirm" class="btn--resetpw" disabled$="[[inProgress]]">
<span>[[localize('lf-resetpw-button')]]</span>
<paper-progress class="btn-progress" hidden$="[[!inProgress]]" disabled$="[[!inProgress]]" indeterminate></paper-progress>
</paper-button>
</template>
</template>
<script>
Polymer({
is: 'login-fire-form',
behaviors: [
Convoo.LocalizeBehavior,
Convoo.LoginFireFormBehavior
],
properties: {
/**
* When `true`, signs up a user who attempts to sign in with an account
* that doesn't already exist.
*
* @type {Boolean}
*/
autoSignUp: {
type: Boolean,
value: false
},
/**
* Federated identity provider ID to use for Firebase Authentication.
*
* @type {String}
* @default 'email'
*/
provider: {
type: String,
readOnly: true,
notify: false,
value: 'email'
},
/**
* Email field value.
*
* @type {String}
*/
email: {
type: String
},
/**
* Regular expression used to check email address format. It's a simplified
* version that check the email address has the same syntax as described
* on <a href="https://en.wikipedia.org/wiki/Email_address">Wikipedia</a>.
* Pay attention, quoted email addresses are not passing the validation due
* to the complexity of the regular expression it should have been write.
*
* Email address syntax validation can be disabled by setting this property
* to "none", "null" or a falsy value.
*
* @type {String}
*/
emailRegex: {
type: String,
value: "^[\\w\"]{1}([\\.!#$%&'*+-/=?^_`{|}~ ]?\\w){0,63}@\\w+([\\.-]?\\w+)*(\\.\\w{2,24})+$",
observer: '_emailRegexChanged',
notify: true,
},
/**
* Password field value.
*
* @type {String}
*/
password: {
type: String
},
/**
* Regular expression used to check password strength. The default Regex
* checks if the password has a length at least of 8 chars and contains:
* - 2 uppercase letters
* - 1 special char letter [!@#$&*]
* - 2 digits
* - 3 lowercase letters
*
* Password strength validation can be disabled by setting this property
* to "none", "null" or a falsy value.
*
* @type {String}
*/
passwordRegex: {
type: String,
value: '^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8,}$',
observer: '_passwordRegexChanged',
notify: true,
},
/**
* Password confirmation field value.
*
* @type {String}
*/
_passwordAgain: {
type: String
},
/**
* Computed property to show sign up form.
*
* @type {Boolean}
*/
_showSignUp: {
type: Boolean,
computed: '_computeShowSignUp(showSignUp, noSignUp)'
},
/**
* Last error message.
*
* @type {String}
*/
_lastError: {
type: String
},
/**
* Last info message.
*
* @type {String}
*/
_lastMessage: {
type: String
}
},
listeners: {
'error': '_handleError',
'signedin': '_handleSignIn',
'signedout': '_handleSignOut',
'reset-password-email-sent': '_handleResetPasswordEmailSent'
},
/**
* Empties the form. Fires a `cancel` event.
*/
cancel: function() {
this._cleanInputs();
this.fire('cancel');
},
/**
* Confirms the form. Depending of the form state, it tries to sign-in,
* sign-up or reset the password.
*/
confirm: function() {
let validation = this.validate();
if (!validation.passed) {
return;
}
if (this._showSignUp) {
this._signUp();
} else if (this.showResetPassword) {
this.sendPasswordResetEmail();
} else if (!this._showSignUp) {
this._signIn()
}
this.fire('confirm');
},
/**
* Validates the email field. This is a basic validation. The only one way
* to be sure the value is a true email address is to send it an email.
*
* @return {Boolean} true if the provided email address has a good format.
*/
validateEmail: function() {
let input = !Polymer.Element ? this.querySelector('#emailInput') :
this.root.querySelector('#emailInput');
return input.validate();
},
/**
* Validates the password field.
*
* @return {Boolean} true if the password value pass the RegExp
* `passwordRegex`.
*/
validatePassword: function() {
let input = !Polymer.Element ? this.querySelector('#passwordInput') :
this.root.querySelector('#passwordInput');
return !!this.showResetPassword || input.validate() &&
(!this.reenterPassword || input.value == this._passwordAgain.value);
},
/**
* Validates the form. Checks if the email address and the password are both
* passing their validation.
*
* @return {Object{passed: Boolean, error?: Object}} the validation result.
*/
validate: function() {
let isEmailValid = this.validateEmail();
let isPasswordValid = this.validatePassword();
let validation = {
passed: isEmailValid && isPasswordValid
};
return validation.passed ? validation : Object.assign({}, validation, {
error: {
code: `form/invalid-${isEmailValid ? 'password' : 'email'}`,
message: `${isEmailValid ?
'The password does not pass the validation.' :
'The email address is not valid.'} The form was not sent.`
}
});
},
_emailRegexChanged(regex) {
if (typeof regex == 'string' &&
(!regex || regex == 'none' || regex == 'null')) {
this.set('emailRegex', null);
}
},
_passwordRegexChanged: function(regex) {
if (typeof regex == 'string' &&
(!regex || regex == 'none' || regex == 'null')) {
this.set('passwordRegex', null);
}
},
/**
* Cleans message and error sections.
*/
_cleanMessages: function() {
this.set('_lastError', '');
this.set('_lastMessage', '');
},
/**
* Cleans email and password inputs.
*/
_cleanInputs: function() {
this.$$('#emailInput').value = '';
this._cleanPasswordInput();
},
/**
* Cleans password input
*/
_cleanPasswordInput: function() {
let input = this.$$('#passwordInput');
if (input) {
input.value = '';
}
},
/**
* Calculates whether to show the sign up section. Returns `true` if the
* form allows signing up and the element is asked to display the according
* section.
*/
_computeShowSignUp: function(showSignUp, noSignUp) {
return showSignUp && !noSignUp;
},
/**
* Tries to sign in a user using the form values.
*/
_signIn: function() {
this._cleanMessages();
if (this.validateEmail() && this.validatePassword()) {
this.signIn(this.email, this.password);
}
},
/**
* Tries to sign up a user using the form values.
*/
_signUp: function(e) {
this._cleanMessages();
if (!this.validateEmail() || !this.validatePassword()) {
if (this.reenterPassword) {
const repeatPwInput = this.$$('#repeatPasswordInput');
repeatPwInput.invalid = !repeatPwInput.validate() || this.password != this._passwordAgain;
return;
}
}
this.signUp(this.email, this.password);
this._cleanInputs();
},
/**
* Handler called after a successful `signIn` to clean form inputs.
*/
_handleSignIn: function() {
this._cleanInputs();
},
/**
* Handler called after a successful `signOut`. Switches back to the `sign-in`
* view if necessary.
*/
_handleSignOut: function() {
if (this.showSignUp) {
this._toggleShowSignUp();
}
},
_handleResetPasswordEmailSent: function() {
this.showResetPassword = false;
},
/**
* Displays error message. If the attribute `autoSignUp` is `true`, before
* to display the error message, a sign up process is tried.
*
* @param {Object} e the event caught that contains the error message
*/
_handleError: function(e) {
let errorCode = e.detail.code;
if (this.autoSignUp && errorCode === 'auth/user-not-found') {
this.signUp(this.email, this.password);
this._cleanInputs();
return;
}
// Display error message.
var errorMessage = this.localize('lf-error-' + errorCode);
this.set('_lastError', errorMessage || e.detail.message);
if (['auth/wrong-password', 'auth/user-not-found'].includes(errorCode)) {
this._cleanPasswordInput();
}
},
/**
* Sends an email for resetting the account password. The email is sent to
* the address entered in the form when the user clicks on the link to reset
* its password.
*/
sendPasswordResetEmail: function() {
this._cleanMessages();
if (this.validateEmail()) {
this._setInProgress(true);
this._auth.sendPasswordResetEmail(this.email)
.then(() => {
this.set('_lastMessage', this.localize('lf-resetpw-success-message'));
this.fire('reset-password-email-sent');
},
(error) => this.set('_lastError', error.message))
.finally(()=> this._setInProgress(false));
}
},
/**
* Checks for enter being pressed and submits the appropriate form action
*/
_submitOnEnter: function(e) {
if (e.keyCode === 13) {
this.confirm();
}
},
/**
* Reveals the password in the parent by switching the type to text
*/
_revealPW: function(e) {
this.$$('#passwordIconButton').icon = 'login-fire-icons:visibility-off';
this.$$('#passwordInput').type = 'text';
},
/**
* Hides the password in the parent by switching the type to password
*/
_hidePW: function(e) {
setTimeout(() => {
this.$$('#passwordIconButton').icon = 'login-fire-icons:visibility';
this.$$('#passwordInput').type = 'password';
}, 1000);
}
/**
* Fired when the form is canceled. At this time, inputs are cleaned.
*
* @event cancel
*/
/**
* Fired when the form is confirmed. At this time, the sign-in, sign-up or
* reset password process is just started.
*
* @event confirm
*/
/**
* Fired when an email to reset the password is sent.
*
* @event reset-password-email-sent
*/
});
</script>
</dom-module>