@@ -114,7 +114,7 @@ const providersIcons = {
114
114
} ;
115
115
116
116
/**
117
- * Returns the active user (i.e. currentUser or lastUser).
117
+ * Returns active user (currentUser or lastUser).
118
118
* @return {!firebase.User }
119
119
*/
120
120
function activeUser ( ) {
@@ -126,63 +126,88 @@ function activeUser() {
126
126
}
127
127
}
128
128
129
+ /**
130
+ * Blocks until there is a valid user
131
+ * then returns the valid user (currentUser or lastUser).
132
+ * @return {!firebase.User }
133
+ */
134
+ async function getActiveUserBlocking ( ) {
135
+ const type = $ ( 'input[name=toggle-user-selection]:checked' ) . val ( ) ;
136
+ if ( type === 'lastUser' ) {
137
+ return lastUser ;
138
+ } else {
139
+ try {
140
+ await auth . authStateReady ( ) ;
141
+ return auth . currentUser ;
142
+ } catch ( e ) {
143
+ log ( e ) ;
144
+ }
145
+ }
146
+ }
147
+
129
148
/**
130
149
* Refreshes the current user data in the UI, displaying a user info box if
131
150
* a user is signed in, or removing it.
132
151
*/
133
- function refreshUserData ( ) {
134
- if ( activeUser ( ) ) {
135
- const user = activeUser ( ) ;
136
- $ ( '.profile' ) . show ( ) ;
137
- $ ( 'body' ) . addClass ( 'user-info-displayed' ) ;
138
- $ ( 'div.profile-email,span.profile-email' ) . text ( user . email || 'No Email' ) ;
139
- $ ( 'div.profile-phone,span.profile-phone' ) . text (
140
- user . phoneNumber || 'No Phone'
141
- ) ;
142
- $ ( 'div.profile-uid,span.profile-uid' ) . text ( user . uid ) ;
143
- $ ( 'div.profile-name,span.profile-name' ) . text ( user . displayName || 'No Name' ) ;
144
- $ ( 'input.profile-name' ) . val ( user . displayName ) ;
145
- $ ( 'input.photo-url' ) . val ( user . photoURL ) ;
146
- if ( user . photoURL != null ) {
147
- let photoURL = user . photoURL ;
148
- // Append size to the photo URL for Google hosted images to avoid requesting
149
- // the image with its original resolution (using more bandwidth than needed)
150
- // when it is going to be presented in smaller size.
151
- if (
152
- photoURL . indexOf ( 'googleusercontent.com' ) !== - 1 ||
153
- photoURL . indexOf ( 'ggpht.com' ) !== - 1
154
- ) {
155
- photoURL = photoURL + '?sz=' + $ ( 'img.profile-image' ) . height ( ) ;
152
+ async function refreshUserData ( ) {
153
+ try {
154
+ let user = await getActiveUserBlocking ( ) ;
155
+ if ( user ) {
156
+ $ ( '.profile' ) . show ( ) ;
157
+ $ ( 'body' ) . addClass ( 'user-info-displayed' ) ;
158
+ $ ( 'div.profile-email,span.profile-email' ) . text ( user . email || 'No Email' ) ;
159
+ $ ( 'div.profile-phone,span.profile-phone' ) . text (
160
+ user . phoneNumber || 'No Phone'
161
+ ) ;
162
+ $ ( 'div.profile-uid,span.profile-uid' ) . text ( user . uid ) ;
163
+ $ ( 'div.profile-name,span.profile-name' ) . text (
164
+ user . displayName || 'No Name'
165
+ ) ;
166
+ $ ( 'input.profile-name' ) . val ( user . displayName ) ;
167
+ $ ( 'input.photo-url' ) . val ( user . photoURL ) ;
168
+ if ( user . photoURL != null ) {
169
+ let photoURL = user . photoURL ;
170
+ // Append size to the photo URL for Google hosted images to avoid requesting
171
+ // the image with its original resolution (using more bandwidth than needed)
172
+ // when it is going to be presented in smaller size.
173
+ if (
174
+ photoURL . indexOf ( 'googleusercontent.com' ) !== - 1 ||
175
+ photoURL . indexOf ( 'ggpht.com' ) !== - 1
176
+ ) {
177
+ photoURL = photoURL + '?sz=' + $ ( 'img.profile-image' ) . height ( ) ;
178
+ }
179
+ $ ( 'img.profile-image' ) . attr ( 'src' , photoURL ) . show ( ) ;
180
+ } else {
181
+ $ ( 'img.profile-image' ) . hide ( ) ;
156
182
}
157
- $ ( 'img.profile-image' ) . attr ( 'src' , photoURL ) . show ( ) ;
158
- } else {
159
- $ ( 'img.profile-image' ) . hide ( ) ;
160
- }
161
- $ ( '.profile-email-verified' ) . toggle ( user . emailVerified ) ;
162
- $ ( '.profile-email-not-verified' ) . toggle ( ! user . emailVerified ) ;
163
- $ ( '.profile-anonymous' ) . toggle ( user . isAnonymous ) ;
164
- // Display/Hide providers icons.
165
- $ ( '.profile-providers' ) . empty ( ) ;
166
- if ( user [ 'providerData' ] && user [ 'providerData' ] . length ) {
167
- const providersCount = user [ 'providerData' ] . length ;
168
- for ( let i = 0 ; i < providersCount ; i ++ ) {
169
- addProviderIcon ( user [ 'providerData' ] [ i ] [ 'providerId' ] ) ;
183
+ $ ( '.profile-email-verified' ) . toggle ( user . emailVerified ) ;
184
+ $ ( '.profile-email-not-verified' ) . toggle ( ! user . emailVerified ) ;
185
+ $ ( '.profile-anonymous' ) . toggle ( user . isAnonymous ) ;
186
+ // Display/Hide providers icons.
187
+ $ ( '.profile-providers' ) . empty ( ) ;
188
+ if ( user [ 'providerData' ] && user [ 'providerData' ] . length ) {
189
+ const providersCount = user [ 'providerData' ] . length ;
190
+ for ( let i = 0 ; i < providersCount ; i ++ ) {
191
+ addProviderIcon ( user [ 'providerData' ] [ i ] [ 'providerId' ] ) ;
192
+ }
193
+ }
194
+ // Show enrolled second factors if available for the active user.
195
+ showMultiFactorStatus ( user ) ;
196
+ // Change color.
197
+ if ( user === auth . currentUser ) {
198
+ $ ( '#user-info' ) . removeClass ( 'last-user' ) ;
199
+ $ ( '#user-info' ) . addClass ( 'current-user' ) ;
200
+ } else {
201
+ $ ( '#user-info' ) . removeClass ( 'current-user' ) ;
202
+ $ ( '#user-info' ) . addClass ( 'last-user' ) ;
170
203
}
171
- }
172
- // Show enrolled second factors if available for the active user.
173
- showMultiFactorStatus ( user ) ;
174
- // Change color.
175
- if ( user === auth . currentUser ) {
176
- $ ( '#user-info' ) . removeClass ( 'last-user' ) ;
177
- $ ( '#user-info' ) . addClass ( 'current-user' ) ;
178
204
} else {
179
- $ ( '#user-info' ) . removeClass ( 'current-user' ) ;
180
- $ ( '#user-info' ) . addClass ( 'last-user' ) ;
205
+ $ ( '.profile' ) . slideUp ( ) ;
206
+ $ ( 'body' ) . removeClass ( 'user-info-displayed' ) ;
207
+ $ ( 'input.profile-data' ) . val ( '' ) ;
181
208
}
182
- } else {
183
- $ ( '.profile' ) . slideUp ( ) ;
184
- $ ( 'body' ) . removeClass ( 'user-info-displayed' ) ;
185
- $ ( 'input.profile-data' ) . val ( '' ) ;
209
+ } catch ( error ) {
210
+ log ( error ) ;
186
211
}
187
212
}
188
213
@@ -456,7 +481,7 @@ function onReauthenticateWithEmailAndPassword() {
456
481
reauthenticateWithCredential ( activeUser ( ) , credential ) . then ( result => {
457
482
logAdditionalUserInfo ( result ) ;
458
483
refreshUserData ( ) ;
459
- alertSuccess ( 'User reauthenticated with email/password! ' ) ;
484
+ alertSuccess ( 'User reauthenticated with email/password' ) ;
460
485
} , onAuthError ) ;
461
486
}
462
487
@@ -1050,7 +1075,7 @@ function onApplyActionCode() {
1050
1075
* or not.
1051
1076
*/
1052
1077
function getIdToken ( forceRefresh ) {
1053
- if ( activeUser ( ) == null ) {
1078
+ if ( ! activeUser ( ) ) {
1054
1079
alertError ( 'No user logged in.' ) ;
1055
1080
return ;
1056
1081
}
@@ -1075,7 +1100,7 @@ function getIdToken(forceRefresh) {
1075
1100
* or not
1076
1101
*/
1077
1102
function getIdTokenResult ( forceRefresh ) {
1078
- if ( activeUser ( ) == null ) {
1103
+ if ( ! activeUser ( ) ) {
1079
1104
alertError ( 'No user logged in.' ) ;
1080
1105
return ;
1081
1106
}
0 commit comments