-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfirebase-auth-manager.html
218 lines (197 loc) · 6.93 KB
/
firebase-auth-manager.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
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../paper-toast/paper-toast.html">
<link rel="import" href="../polymerfire/firebase-auth.html">
<link rel="import" href="../polymerfire/firebase-document.html">
<!--
`<firebase-auth-manager>` is a wrapper element to `<firebase-auth>` that listens to the various `sign-XXX` event and delegate their execution.
Whenever an error occurs during a firebase operation, it displays an error dialog with the corresponding error message. For successful operation, it shows a toast.
@group GoogleWebComponents Elements
@element firebase-auth-manager
@hero hero.svg
@demo demo/index.html
-->
<dom-module id="firebase-auth-manager">
<template>
<firebase-auth id="firebaseAuth"
user="{{_user}}"
signed-in="{{logged}}">
</firebase-auth>
<firebase-document id="firebaseAuthDoc"
path="[[_profilePath]]"
data="{{profile}}">
</firebase-document>
<!-- TODO PG: eventually write a firebase-toast and extract it-->
<paper-toast id="toast" text="[[_toastMessage]]" duration="3000"></paper-toast>
</template>
<script>
class FirebaseAuthManager extends Polymer.Element {
static get is() { return 'firebase-auth-manager' }
static get properties() {
return {
/**
* Logged used for the `<firebase-auth>` inner element
*/
logged: {
type: Boolean,
notify: true,
observer: '_notifyStatus'
},
/**
* Profile base path used for the profile firebase document
*/
profileBasePath: {
type: String,
value: '/profiles',
notify: true
},
/**
* Value of the profile firebase document
*/
profile: {
type: Object,
notify: true
},
/**
* Computed path used for the profile firebase document
*/
_profilePath: {
type: String,
computed: '_computeProfilePath(profileBasePath, _user)'
},
/**
* User used for the `<firebase-auth>` inner element
*/
_user: {
type: Object,
value: null
},
/**
* Text message of the toast
*/
_toastMessage: {
type: String
}
}
}
static get observers() {
return [
'_profileChanged(profile, _user)',
]
}
ready() {
window.addEventListener('sign-up', this._signUpHandler.bind(this))
window.addEventListener('sign-in', this._signInHandler.bind(this))
window.addEventListener('sign-out', this._signOutHandler.bind(this))
window.addEventListener('user-created', this._userHandler.bind(this))
window.addEventListener('login', this._userHandler.bind(this))
window.addEventListener('logout', this._userHandler.bind(this))
window.addEventListener('error', this._errorHandler.bind(this))
super.ready()
}
disconnectedCallback() {
window.removeEventListener('sign-up', this._signUpHandler.bind(this));
window.removeEventListener('sign-in', this._signInHandler.bind(this));
window.removeEventListener('sign-out', this._signOutHandler.bind(this));
window.removeEventListener('user-created', this._userHandler.bind(this));
window.removeEventListener('login', this._userHandler.bind(this));
window.removeEventListener('logout', this._userHandler.bind(this));
window.removeEventListener('error', this._errorHandler.bind(this));
super.disconnectedCallback()
}
_notifyStatus(newStatus, oldStatus) {
if (oldStatus && !newStatus) {
this.dispatchEvent(new CustomEvent('logout', { bubbles: true, composed: true }))
}
if (newStatus && !oldStatus) {
this.dispatchEvent(new CustomEvent('login', { bubbles: true, composed: true }));
}
}
_computeProfilePath(profileBasePath, user) {
if (this.logged) {
return [profileBasePath, user.uid].join('/')
} else {
return [profileBasePath, 'undefined'].join('/')
}
}
_profileChanged(profile, user) {
if (user && profile && Object.keys(profile).length === 0) {
var providerInfo = user.providerData;
var i;
profile.displayName = user.displayName;
if (!profile.displayName) {
for (i=0; i<providerInfo.length; i++) {
profile.displayName = providerInfo[i].displayName;
if (profile.displayName) break;
}
profile.displayName = profile.displayName || '';
}
profile.email = user.email;
if (!profile.email) {
for (i=0; i<providerInfo.length; i++) {
profile.email = providerInfo[i].email;
if (profile.email) break;
}
profile.email = profile.email || '';
}
profile.picture = user.photoURL;
if (!profile.picture) {
for (i=0; i<providerInfo.length; i++) {
profile.picture = providerInfo[i].photoURL;
if (profile.picture) break;
}
profile.picture = profile.picture || '';
}
this.$.firebaseAuthDoc.saveValue(this.profileBasePath, user.uid);
this.set('profile', profile);
}
}
_signUpHandler(e) {
this.$.firebaseAuth.createUserWithEmailAndPassword(
e.detail.params.email,
e.detail.params.password
);
}
_signInHandler(e) {
this.$.firebaseAuth.provider = e.detail.provider;
if (e.detail.provider == "password") {
this.$.firebaseAuth.signInWithEmailAndPassword(
e.detail.params.email,
e.detail.params.password
);
}
else {
this.$.firebaseAuth.signInWithPopup();
}
}
_signOutHandler() {
this.$.firebaseAuth.signOut();
// this.async(function() {
// this.set('profile', undefined);
// });
}
_userHandler(e) {
switch (e.type) {
case 'user-created':
this._toastMessage = 'Sign-Up ';
break;
case 'login':
this._toastMessage = 'Sign-In ';
break;
case 'logout':
this._toastMessage = 'Sign-Out ';
break;
}
this._toastMessage += 'successful!';
this.$.toast.open();
}
_errorHandler(e) {
if (e.detail && e.detail.code == 'auth/popup-blocked') {
this.$.firebaseAuth.signInWithRedirect();
return;
}
console.error('Unknown error: ' + e.detail);
}
}
customElements.define(FirebaseAuthManager.is, FirebaseAuthManager)
</script>
</dom-module>