-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfirebase-auth-page.html
303 lines (264 loc) · 9.18 KB
/
firebase-auth-page.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
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../polymer/lib/elements/dom-if.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="../paper-button/paper-button.html">
<link rel="import" href="../paper-input/paper-input.html">
<link rel="import" href="../gold-password-input/gold-password-input.html">
<link rel="import" href="../gold-password-input/gold-password-input-validator.html">
<link rel="import" href="./firebase-icons.html">
<!--
`<firebase-auth-page>` is a page with Material Design styling and optional animations that leverages firebase authentication in a flexible way. You can:
- setup a 'Sign-Up' or 'Sigh-In' flavor.
- configure the providers accessible within the page.
- configure the header e.g. a <h2></h2> with a "header" attribute
- configure different sections: "introduction", "provider" and "user" thanks to the corresponding attribute.
Example:
<firebase-auth-page
providers='["facebook", "github", "google"]'>
<h2 slot="title-sign-up">Sign-Up Page</h2>
<h2 slot="title-sign-in">Sign-In Page</h2>
<span slot="introduction">-- Welcome to the Firebase Auth Page Demo: --</span>
<span slot="provider">-- Login with one of the following provider(s): --</span>
<span slot="user">-- or provide your email and password : --</span>
</firebase-auth-page>
@element firebase-auth-page
@demo demo/index.html
-->
<dom-module id="firebase-auth-page">
<template>
<style>
:host {
display: block;
}
:host > ::slotted(*:first-child) {
margin-top: 0;
}
.facebook {
color: white;
--iron-icon-fill-color: white;
background-color: #3B5998;
}
.github {
color: black;
background-color: white;
}
.google {
color: white;
--iron-icon-fill-color: white;
background-color: #DC4E41;
}
.twitter {
color: white;
--iron-icon-fill-color: white;
background-color: #4099FF;
}
#introduction-section, #provider-section, #user-section {
text-align: center;
}
#providers {
@apply --layout-horizontal;
@apply --layout-center-center;
}
</style>
<template is="dom-if" if="[[_isSignUp(type)]]" restamp>
<slot name="title-sign-up"></slot>
</template>
<template is="dom-if" if="[[_isSignIn(type)]]" restamp>
<slot name="title-sign-in"></content>
</template>
<div id="introduction-section">
<slot name="introduction" id="introduction"></slot>
</div>
<div id="provider-section" hidden$="[[_computeProviderHidden(providers)]]">
<slot name="provider" id="provider"></slot>
</div>
<div id="providers">
<template is="dom-repeat" items="{{providers}}" as="provider">
<paper-button raised
class$="[[provider]]"
provider$="[[provider]]"
on-tap="_handleProvider">
<iron-icon class$="[[provider]]" icon="firebase:[[provider]]"></iron-icon>
[[provider]]
</paper-button>
</template>
</div>
<div id="user-section">
<slot name="user" id="user"></slot>
<!-- Email input -->
<paper-input id="email"
type="email"
invalid="{{emailInvalid}}"
auto-validate
error-message="Please, enter a valid email">
</paper-input>
<!-- Primary password input -->
<gold-password-input id="primary"
reveal
value="{{primary}}"
invalid="{{primaryInvalid}}"
auto-validate
error-message="The password must contain at least 6 characters">
</gold-password-input>
<!-- Verification password input -->
<gold-password-input-validator
pattern="[[primary]]">
</gold-password-input-validator>
<gold-password-input id="verification"
reveal
invalid="{{verificationInvalid}}"
auto-validate
validator="gold-password-input-validator"
error-message="The verification password you entered is different!">
</gold-password-input>
</div>
<div class="buttons">
<paper-button raised
disabled="[[_computeConfirmDisabled(type,emailInvalid,primary,primaryInvalid,verificationInvalid)]]"
on-tap="_handleConfirm">
[[_computeConfirm(type)]]
</paper-button>
<paper-button raised
on-tap="_handleCancel">
Cancel
</paper-button>
</div>
</template>
<script>
(function() {
/**
* Enum dialog type values.
* @readonly
* @enum {string}
*/
var DialogTypeValue = {
SIGNUP: 'SignUp',
SIGNIN: 'SignIn'
};
class FirebaseAuthPage extends Polymer.Element {
static get is() { return 'firebase-auth-page' }
static get properties() {
return {
/**
* `type` indicates the type of authentication dialog to use. It can hold currently two
* values, namely DialogTypeValue.SIGNUP and DialogTypeValue.SIGNIN.
*
* @type {string}
*/
type: {
type: String,
observer: '_typeChanged'
},
/**
* `providers` set the list of providers accessible for SignUp/SignIn.
*
* @type {Array}
*/
providers: {
type: Array,
value: function() { return []; },
observer: '_providersChanged'
}
}
}
//
// Element Behavior
//
_typeChanged() {
switch(this.type) {
case DialogTypeValue.SIGNUP:
this.$.primary.setAttribute('strength-meter', '');
this.$.primary.setAttribute('minlength', '6');
this.$.primary.setAttribute('autocomplete', 'new-password');
this.$.verification.removeAttribute('hidden');
break;
case DialogTypeValue.SIGNIN:
this.$.primary.removeAttribute('strength-meter');
this.$.primary.removeAttribute('minlength');
this.$.primary.setAttribute('autocomplete', 'current-password');
this.$.verification.setAttribute('hidden', '');
break;
default:
console.error('Invalid dialog type!');
}
}
_providersChanged() {
// TODO PG: check the list of provider given and filter only the supported one
}
_isSignUp(type) {
return type === DialogTypeValue.SIGNUP;
}
_isSignIn(type) {
return type === DialogTypeValue.SIGNIN;
}
_computeProviderHidden(provider) {
return provider.length === 0;
}
_computeConfirm(type) {
switch(type) {
case DialogTypeValue.SIGNUP:
case DialogTypeValue.SIGNIN:
return type;
default:
console.error('Invalid dialog type!');
}
}
_computeConfirmDisabled(type, emailInvalid, primary, primaryInvalid, verificationInvalid) {
switch(type) {
case DialogTypeValue.SIGNUP:
if (this.$.verification.value === '' || verificationInvalid) {
return true;
}
break;
case DialogTypeValue.SIGNIN:
if (this.$.email.value === '' || emailInvalid) {
return true;
}
if (primary === '' || primaryInvalid) {
return true;
}
break;
}
return false;
}
_handleProvider(e) {
if (e && e.target && e.target.getAttribute('provider')) {
this.dispatchEvent(new CustomEvent('sign-in',
{ bubbles: true, composed: true, detail: { provider: e.target.getAttribute('provider') } }));
}
}
_handleConfirm() {
var params = {};
params.email = this.$.email.value;
params.password = this.$.primary.value;
switch(this.type) {
case DialogTypeValue.SIGNUP:
this.dispatchEvent(new CustomEvent('sign-up',
{ bubbles: true, composed: true, detail: { provider: 'password', params: params } }));
break;
case DialogTypeValue.SIGNIN:
this.dispatchEvent(new CustomEvent('sign-in',
{ bubbles: true, composed: true, detail: { provider: 'password', params: params } }));
break;
}
}
_handleCancel() {
this.$.email.value = '';
this.$.primary.value = '';
this.$.verification.value = '';
}
}
/**
Fired when a user confirm a Sign-Up.
@event sign-up
@param {{params: Object}} detail Contains user and password info.
*/
/**
Fired when a user confirm a Login.
@event sign-in
@param {{ provider: string, params: Object}} detail Contains user and password info.
*/
customElements.define(FirebaseAuthPage.is, FirebaseAuthPage);
}());
</script>
</dom-module>