Skip to content

Commit b6b8aed

Browse files
wti806bojeil-google
authored andcommitted
- Support Continue as guest and add new sign in provider firebaseui.auth.AnonymousAuthProvider. (#458)
- Add the ability to configure country selection for phone Auth. - Add return type for `start()` and `setConfig()` in typescript declaration. PiperOrigin-RevId: 205712709 Change-Id: Ie3d1a50be4fbd95b8ba02d1c6f500f553ff4ba60
1 parent beb82a4 commit b6b8aed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1853
-275
lines changed

README.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ remembering emails
134134
7. Integration with
135135
[one-tap sign-up](https://developers.google.com/identity/one-tap/web/overview)
136136
8. Ability to upgrade anonymous users through sign-in/sign-up.
137+
9. Sign-in as a guest
137138

138139
### Configuring sign-in providers
139140

@@ -147,6 +148,7 @@ provider you want to use in their own developer app settings. Please read the
147148
- [Facebook](https://firebase.google.com/docs/auth/web/facebook-login#before_you_begin)
148149
- [Twitter](https://firebase.google.com/docs/auth/web/twitter-login#before_you_begin)
149150
- [Github](https://firebase.google.com/docs/auth/web/github-auth#before_you_begin)
151+
- [Anonymous](https://firebase.google.com/docs/auth/web/anonymous-auth#before_you_begin)
150152

151153
### Starting the sign-in flow
152154

@@ -187,10 +189,13 @@ for a more in-depth example, showcasing a Single Page Application mode.
187189
firebase.auth.TwitterAuthProvider.PROVIDER_ID,
188190
firebase.auth.GithubAuthProvider.PROVIDER_ID,
189191
firebase.auth.EmailAuthProvider.PROVIDER_ID,
190-
firebase.auth.PhoneAuthProvider.PROVIDER_ID
192+
firebase.auth.PhoneAuthProvider.PROVIDER_ID,
193+
firebaseui.auth.AnonymousAuthProvider.PROVIDER_ID
191194
],
192195
// Terms of service url.
193-
tosUrl: '<your-tos-url>'
196+
tosUrl: '<your-tos-url>',
197+
// Privacy policy url.
198+
privacyPolicyUrl: '<your-privacy-policy-url>'
194199
};
195200
196201
// Initialize the FirebaseUI Widget using Firebase.
@@ -396,6 +401,11 @@ FirebaseUI supports the following configuration parameters.
396401
<td>Yes</td>
397402
<td>The URL of the Terms of Service page.</td>
398403
</tr>
404+
<tr>
405+
<td>privacyPolicyUrl</td>
406+
<td>Yes</td>
407+
<td>The URL of the privacy policy page.</td>
408+
</tr>
399409
</tbody>
400410
</table>
401411

@@ -495,6 +505,7 @@ To see FirebaseUI in action with one-tap sign-up, check out the FirebaseUI
495505
|Github |`firebase.auth.GithubAuthProvider.PROVIDER_ID` |
496506
|Email and password|`firebase.auth.EmailAuthProvider.PROVIDER_ID` |
497507
|Phone number |`firebase.auth.PhoneAuthProvider.PROVIDER_ID` |
508+
|Anonymous |`firebaseui.auth.AnonymousAuthProvider.PROVIDER_ID`|
498509

499510
### Configure OAuth providers
500511

@@ -595,6 +606,25 @@ ui.start('#firebaseui-auth-container', {
595606
});
596607
```
597608

609+
### Configure Anonymous Provider
610+
611+
The `AnonymousAuthProvider` can be enabled to let users continue as a guest. If
612+
a user is already signed in anonymously, clicking this sign-in option will keep
613+
the same current anonymous user. In addition, when auto-upgrade for anonymous
614+
users is enabled in addition to this option and a user is already signed in
615+
anonymously, clicking this sign-in option will also keep the same current
616+
anonymous user.
617+
618+
```javascript
619+
ui.start('#firebaseui-auth-container', {
620+
signInOptions: [
621+
{
622+
provider: firebaseui.auth.AnonymousAuthProvider.PROVIDER_ID
623+
}
624+
]
625+
});
626+
```
627+
598628
### Sign In Flows
599629

600630
Two sign in flows are available:
@@ -756,10 +786,13 @@ FirebaseUI is displayed.
756786
size: 'invisible',
757787
badge: 'bottomleft'
758788
}
759-
}
789+
},
790+
firebaseui.auth.AnonymousAuthProvider.PROVIDER_ID
760791
],
761792
// Terms of service url.
762-
tosUrl: '<your-tos-url>'
793+
tosUrl: '<your-tos-url>',
794+
// Privacy policy url.
795+
privacyPolicyUrl: '<your-privacy-policy-url>'
763796
};
764797
765798
var ui = new firebaseui.auth.AuthUI(firebase.auth());
@@ -1081,6 +1114,7 @@ Firebase core SDK.
10811114
|Twitter |`firebase.auth.TwitterAuthProvider.PROVIDER_ID` |
10821115
|Github |`firebase.auth.GithubAuthProvider.PROVIDER_ID` |
10831116
|Email and password|`firebase.auth.EmailAuthProvider.PROVIDER_ID` |
1117+
|Anonymous |`firebaseui.auth.AnonymousAuthProvider.PROVIDER_ID`|
10841118

10851119
### Setup and Usage
10861120

demo/public/app.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ function getUiConfig() {
6868
recaptchaParameters: {
6969
size: getRecaptchaMode()
7070
}
71-
}
71+
},
72+
firebaseui.auth.AnonymousAuthProvider.PROVIDER_ID
7273
],
7374
// Terms of service url.
7475
'tosUrl': 'https://www.google.com',

demo/public/widget.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
recaptchaParameters: {
5151
size: getRecaptchaMode()
5252
}
53-
}
53+
},
54+
firebaseui.auth.AnonymousAuthProvider.PROVIDER_ID
5455
],
5556
// Terms of service url.
5657
'tosUrl': 'https://www.google.com',

externs/firebaseui-externs.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* The FirebaseUI namespace.
2323
* @namespace
2424
*/
25-
firebaseui = {};
25+
var firebaseui = {};
2626

2727
/**
2828
* The FirebaseUI auth namespace.
@@ -31,6 +31,20 @@ firebaseui = {};
3131
firebaseui.auth = {};
3232

3333

34+
/**
35+
* The FirebaseUI Anonymous Auth Provider namespace.
36+
* @constructor
37+
*/
38+
firebaseui.auth.AnonymousAuthProvider = {};
39+
40+
41+
/**
42+
* The FirebaseUI Anonymous Auth Provider ID.
43+
* @const {string}
44+
*/
45+
firebaseui.auth.AnonymousAuthProvider.PROVIDER_ID = 'anonymous';
46+
47+
3448
/**
3549
* @param {!firebase.auth.Auth} auth The Firebase Auth instance.
3650
* @param {string=} appId The optional app id.
@@ -401,3 +415,25 @@ firebaseui.auth.SignInOption.prototype.defaultNationalNumber;
401415
* @type {string|undefined}
402416
*/
403417
firebaseui.auth.SignInOption.prototype.loginHint;
418+
419+
/**
420+
* Sets the whitelisted countries. Accept either ISO (alpha-2) or E164 formatted
421+
* country codes. Invalid country code will be ignored. If `defaultCountry` is
422+
* provided, it must be whitelisted. `whitelistedCountries` and
423+
* `blacklistedCountries` cannot be specified at the same time.
424+
* Example: ['US', '+44']
425+
*
426+
* @type {!Array<string>|undefined}
427+
*/
428+
firebaseui.auth.SignInOption.prototype.whitelistedCountries;
429+
430+
/**
431+
* Sets the blacklisted countries. Accept either ISO (alpha-2) or E164 formatted
432+
* country codes. Invalid country code will be ignored. If `defaultCountry` is
433+
* provided, it must not be blacklisted. `whitelistedCountries` and
434+
* `blacklistedCountries` cannot be specified at the same time.
435+
* Example: ['US', '+44']
436+
*
437+
* @type {!Array<string>|undefined}
438+
*/
439+
firebaseui.auth.SignInOption.prototype.blacklistedCountries;

image/anonymous.png

551 Bytes
Loading

javascript/data/country.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ firebaseui.auth.data.country.LookupTree.prototype.search = function(code) {
9393
};
9494

9595

96+
/**
97+
* Gets the list of counties used to build up the tree.
98+
* @return {!Array<!firebaseui.auth.data.country.Country>} The list of
99+
* countries to construct the prefix tree for.
100+
*/
101+
firebaseui.auth.data.country.LookupTree.prototype.getCountries = function() {
102+
return this.countries_;
103+
};
104+
105+
96106
/**
97107
* Fetches data about a country given its e164_key field.
98108
* @param {string} key The e164_key of the country.
@@ -128,6 +138,44 @@ firebaseui.auth.data.country.getCountriesByIso2 = function(code) {
128138
};
129139

130140

141+
/**
142+
* Fetches data about a country given its E164 country calling code (e164_cc)
143+
* field. May return multiple entries for countries with multiple
144+
* country codes, or an empty array if the country code was not found.
145+
* @param {string} code The e164_cc of the country.
146+
* @return {!Array<!firebaseui.auth.data.country.Country>}
147+
*/
148+
firebaseui.auth.data.country.getCountriesByE164Code = function(code) {
149+
var countries = [];
150+
for (var i = 0; i < firebaseui.auth.data.country.COUNTRY_LIST.length; i++) {
151+
if (firebaseui.auth.data.country.COUNTRY_LIST[i].e164_cc == code) {
152+
countries.push(firebaseui.auth.data.country.COUNTRY_LIST[i]);
153+
}
154+
}
155+
return countries;
156+
};
157+
158+
159+
/**
160+
* Fetches data about a country given its E164 country calling code (e164_cc)
161+
* prefixed with '+' or ISO 3166-1 alpha-2 country code (iso2_cc). May
162+
* return multiple entries for countries with multiple country codes, or an
163+
* empty array if the country code was not found.
164+
* @param {string} code The e164_cc or iso2_cc of the country.
165+
* @return {!Array<!firebaseui.auth.data.country.Country>}
166+
*/
167+
firebaseui.auth.data.country.getCountriesByE164OrIsoCode = function(code) {
168+
var countries = [];
169+
if (code.length > 0 && code.charAt(0) == '+') {
170+
countries = firebaseui.auth.data.country.getCountriesByE164Code(
171+
code.substring(1));
172+
} else {
173+
countries =
174+
firebaseui.auth.data.country.getCountriesByIso2(code);
175+
}
176+
return countries;
177+
};
178+
131179

132180
/**
133181
* Sorts the list of countries by name for the given locale.

javascript/data/country_test.js

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ function testCountryLookupByPhoneNumber() {
107107
// Initialize lookup tree.
108108
var lookupTree = new firebaseui.auth.data.country.LookupTree(
109109
firebaseui.auth.data.country.COUNTRY_LIST);
110+
assertEquals(
111+
firebaseui.auth.data.country.COUNTRY_LIST, lookupTree.getCountries());
110112
// Empty string.
111113
assertSameElements([], lookupTree.search(''));
112114
// Invalid country code that starts with 0.
@@ -154,7 +156,7 @@ function testGetCountriesByIso2() {
154156
* Tests that getCountriesByIso2 works correctly if multiple entries have the
155157
* same ISO2 code.
156158
*/
157-
function testGetCountriesByIso2_multiple() {
159+
function testGetCountriesByIso2_multipleMatches() {
158160
var countries = firebaseui.auth.data.country.getCountriesByIso2('xk');
159161
var actualCodes = goog.array.map(countries, function(country) {
160162
return country.e164_cc;
@@ -167,3 +169,75 @@ function testGetCountriesByIso2_notFound() {
167169
var countries = firebaseui.auth.data.country.getCountriesByIso2('zz');
168170
assertEquals(0, countries.length);
169171
}
172+
173+
174+
function testGetCountriesByE164Code() {
175+
var countries = firebaseui.auth.data.country.getCountriesByE164Code('86');
176+
assertEquals(1, countries.length);
177+
assertEquals('China', countries[0].name);
178+
assertEquals('CN', countries[0].iso2_cc);
179+
}
180+
181+
182+
/**
183+
* Tests that getCountriesByE164Code works correctly if multiple entries have
184+
* the same e164_cc code.
185+
*/
186+
function testGetCountriesByE164Code_multipleMatches() {
187+
var countries = firebaseui.auth.data.country.getCountriesByE164Code('44');
188+
var actualKeys = goog.array.map(countries, function(country) {
189+
return country.e164_key;
190+
});
191+
assertSameElements(['44-GG-0', '44-IM-0', '44-JE-0', '44-GB-0'], actualKeys);
192+
}
193+
194+
195+
function testGetCountriesByE164Code_notFound() {
196+
var countries = firebaseui.auth.data.country.getCountriesByIso2('999');
197+
assertEquals(0, countries.length);
198+
}
199+
200+
201+
function testGetCountriesByE164OrIsoCode_e164() {
202+
var countries =
203+
firebaseui.auth.data.country.getCountriesByE164OrIsoCode('+86');
204+
assertEquals(1, countries.length);
205+
assertEquals('China', countries[0].name);
206+
assertEquals('CN', countries[0].iso2_cc);
207+
}
208+
209+
210+
function testGetCountriesByE164eOrIsoCode_iso() {
211+
var countries =
212+
firebaseui.auth.data.country.getCountriesByE164OrIsoCode('CN');
213+
assertEquals(1, countries.length);
214+
assertEquals('China', countries[0].name);
215+
assertEquals('86', countries[0].e164_cc);
216+
}
217+
218+
219+
function testGetCountriesByE164OrIsoCode_e164_multipleMatches() {
220+
var countries =
221+
firebaseui.auth.data.country.getCountriesByE164OrIsoCode('+44');
222+
var actualKeys = goog.array.map(countries, function(country) {
223+
return country.e164_key;
224+
});
225+
assertSameElements(['44-GG-0', '44-IM-0', '44-JE-0', '44-GB-0'], actualKeys);
226+
}
227+
228+
229+
function testGetCountriesByE164OrIsoCode_iso_multipleMatches() {
230+
var countries =
231+
firebaseui.auth.data.country.getCountriesByE164OrIsoCode('xk');
232+
var actualKeys = goog.array.map(countries, function(country) {
233+
return country.e164_key;
234+
});
235+
assertSameElements(['377-XK-0', '381-XK-0', '386-XK-0'], actualKeys);
236+
}
237+
238+
239+
function testGetCountriesByE164CodeOrIsoCode_notFound() {
240+
var countries =
241+
firebaseui.auth.data.country.getCountriesByE164OrIsoCode('+999');
242+
assertEquals(0, countries.length);
243+
}

javascript/testing/auth.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ FakeAuthClient.AuthAsyncMethod = {
205205
'signInAndRetrieveDataWithEmailAndPassword',
206206
SIGN_IN_ANONYMOUSLY_AND_RETRIEVE_DATA:
207207
'signInAnonymouslyAndRetrieveData',
208+
SIGN_IN_ANONYMOUSLY: 'signInAnonymously',
208209
SIGN_IN_WITH_CREDENTIAL: 'signInWithCredential',
209210
SIGN_IN_WITH_CUSTOM_TOKEN: 'signInWithCustomToken',
210211
SIGN_IN_WITH_EMAIL_AND_PASSWORD: 'signInWithEmailAndPassword',

0 commit comments

Comments
 (0)