Skip to content

Commit 37f0739

Browse files
Tidy and update Android dependencies
1 parent 98ebfb1 commit 37f0739

6 files changed

Lines changed: 139 additions & 73 deletions

File tree

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ Not all of the above options will function on all platforms:
147147
- theme: a theme identifier for styling - Android only
148148
- logo: a logo to display - Android only
149149
- uiElement: a jQuery selector for web login - Web only
150-
- anonymous : if true log in an an anonymous user upon initialisation (default false)
150+
- anonymous : if true log in an anonymous user if other attempts fail (default false)
151151
- smartLockEnabled : enable SmartLock to store credentials - Android only (default true)
152152
- smartLockHints : enable SmartLock hints - Android only (default false)
153153

@@ -156,7 +156,7 @@ In order for the above initialisation to work on a browser you need to include s
156156

157157
```
158158
FirebaseUIAuth.initialise({
159-
"providers": ["GOOGLE", "FACEBOOK", "EMAIL"],
159+
"providers": ["GOOGLE", "FACEBOOK", "EMAIL", "ANONYMOUS"],
160160
"tosUrl" : "http://www.myapp.co.uk/terms.html",
161161
"privacyPolicyUrl" : "http://www.myapp.co.uk/privacy.html",
162162
"theme" : "themeName",
@@ -176,6 +176,9 @@ FirebaseUIAuth.initialise({
176176
```
177177

178178
# Methods
179+
## signInAnonymously()
180+
Call this to sign in anonymously.
181+
179182
## signIn()
180183
Call this to start the sign in process based on the above configuration. This can raise the following events:
181184

@@ -203,6 +206,9 @@ Sign in failed for some reason. The following is returned:
203206
}
204207
```
205208

209+
## signinaborted
210+
Sign in has been aborted by the user, by closing the sign in window.
211+
206212
## signOut()
207213
Sign the current user out of the application. This can raise the following events:
208214

@@ -313,6 +319,11 @@ In order to ensure the browser implementation works, it will be necessary to con
313319
```
314320

315321
# History
322+
## 1.-.0
323+
- Major Android dependency update to 4.2.1
324+
- Breaking change around meaning of 'anonymous' configuration option
325+
- Add support for ANONYMOUS provider
326+
316327
## 0.0.10
317328
- Update JS Firebase dependencies
318329

plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
<preference name="ANDROID_FIREBASE_CORE_VERSION" default="16.0.4"/>
110110
<preference name="ANDROID_FIREBASE_AUTH_VERSION" default="16.0.4"/>
111111
<preference name="ANDROID_PLAY_SERVICES_AUTH_VERSION" default="16.0.1"/>
112-
<preference name="ANDROID_FIREBASEUI_VERSION" default="3.3.1"/>
112+
<preference name="ANDROID_FIREBASEUI_VERSION" default="4.2.1"/>
113113
<preference name="ANDROID_FACEBOOK_SDK_VERSION" default="4.33.0"/>
114114
<preference name="COLOR_PRIMARY" default="#ffffff"/>
115115
<preference name="COLOR_DARK_PRIMARY" default="#555555"/>

src/android/uk/co/reallysmall/cordova/plugin/firebase/ui/auth/FirebaseUIAuthPlugin.java

Lines changed: 112 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
import android.util.Log;
77

88
import com.firebase.ui.auth.AuthUI;
9+
import com.firebase.ui.auth.ErrorCodes;
10+
import com.firebase.ui.auth.IdpResponse;
911
import com.google.android.gms.tasks.OnCompleteListener;
1012
import com.google.android.gms.tasks.OnFailureListener;
1113
import com.google.android.gms.tasks.OnSuccessListener;
1214
import com.google.android.gms.tasks.Task;
15+
import com.google.firebase.auth.AuthCredential;
1316
import com.google.firebase.auth.AuthResult;
1417
import com.google.firebase.auth.FirebaseAuth;
1518
import com.google.firebase.auth.FirebaseUser;
@@ -28,11 +31,11 @@
2831

2932
import static android.app.Activity.RESULT_OK;
3033

31-
public class FirebaseUIAuthPlugin extends CordovaPlugin implements OnCompleteListener<AuthResult>, FirebaseAuth.AuthStateListener {
34+
public class FirebaseUIAuthPlugin extends CordovaPlugin implements OnCompleteListener<AuthResult> {
3235
private static final int RC_SIGN_IN = 123;
3336
private final String TAG = "FirebaseUIAuthPlugin";
3437
List<AuthUI.IdpConfig> providers = Arrays.asList(
35-
new AuthUI.IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build());
38+
new AuthUI.IdpConfig.EmailBuilder().build());
3639
private CallbackContext callbackContext;
3740
private FirebaseAuth firebaseAuth;
3841
private JSONObject options;
@@ -51,6 +54,8 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
5154
return initialise(args, callbackContext);
5255
} else if ("signIn".equals(action)) {
5356
return signIn(callbackContext);
57+
} else if ("signInAnonymously".equals(action)) {
58+
return signInAnonymously(callbackContext);
5459
} else if ("signOut".equals(action)) {
5560
return signOut(callbackContext);
5661
} else if ("deleteUser".equals(action)) {
@@ -84,6 +89,13 @@ public void onSuccess(Void aVoid) {
8489
return true;
8590
}
8691

92+
private boolean signInAnonymously(CallbackContext callbackContext) {
93+
94+
signInAnonymous(firebaseAuth);
95+
96+
return true;
97+
}
98+
8799
private boolean sendEmailVerification(final CallbackContext callbackContext) {
88100

89101
FirebaseUser user = firebaseAuth.getCurrentUser();
@@ -127,14 +139,14 @@ private boolean initialise(JSONArray args, final CallbackContext callbackContext
127139

128140
firebaseAuth = FirebaseAuth.getInstance();
129141

130-
firebaseAuth.addAuthStateListener(this);
131-
this.signInAnonymous(firebaseAuth);
132-
133142
return true;
134143
}
135144

136145
private void signInAnonymous(final FirebaseAuth firebaseAuth) {
137-
if (firebaseAuth.getCurrentUser() == null && anonymous) {
146+
147+
FirebaseUser user = firebaseAuth.getCurrentUser();
148+
149+
if (user == null && anonymous) {
138150
firebaseAuth.signInAnonymously().addOnCompleteListener(new OnCompleteListener<AuthResult>() {
139151
@Override
140152
public void onComplete(@NonNull Task<AuthResult> task) {
@@ -167,19 +179,25 @@ private void createProviderList() throws JSONException {
167179
Log.d(TAG, "createProviderList: parsing provider " + providerList.getString(i));
168180

169181
if ("EMAIL".equals(providerList.getString(i))) {
170-
providers.add(new AuthUI.IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build());
182+
providers.add(new AuthUI.IdpConfig.EmailBuilder().build());
171183
}
172184
if ("FACEBOOK".equals(providerList.getString(i))) {
173-
providers.add(new AuthUI.IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER).build());
185+
providers.add(new AuthUI.IdpConfig.FacebookBuilder().build());
174186
}
175187
if ("GOOGLE".equals(providerList.getString(i))) {
176-
providers.add(new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build());
188+
providers.add(new AuthUI.IdpConfig.GoogleBuilder().build());
177189
}
178190
if ("PHONE".equals(providerList.getString(i))) {
179-
providers.add(new AuthUI.IdpConfig.Builder(AuthUI.PHONE_VERIFICATION_PROVIDER).build());
191+
providers.add(new AuthUI.IdpConfig.PhoneBuilder().build());
180192
}
181193
if ("TWITTER".equals(providerList.getString(i))) {
182-
providers.add(new AuthUI.IdpConfig.Builder(AuthUI.TWITTER_PROVIDER).build());
194+
providers.add(new AuthUI.IdpConfig.TwitterBuilder().build());
195+
}
196+
if ("GITHUB".equals(providerList.getString(i))) {
197+
providers.add(new AuthUI.IdpConfig.GitHubBuilder().build());
198+
}
199+
if ("ANONYMOUS".equals(providerList.getString(i))) {
200+
providers.add(new AuthUI.IdpConfig.AnonymousBuilder().build());
183201
}
184202
}
185203
}
@@ -199,8 +217,11 @@ private boolean signIn(CallbackContext callbackContext) {
199217
} else {
200218
Log.d(TAG, "signIn: sign in");
201219

220+
cordova.setActivityResultCallback(this);
221+
202222
cordova.getActivity().startActivityForResult(
203223
this.buildCustomInstance()
224+
.enableAnonymousUsersAutoUpgrade()
204225
.setAvailableProviders(providers)
205226
.build(),
206227
RC_SIGN_IN);
@@ -220,7 +241,7 @@ private AuthUI.SignInIntentBuilder buildCustomInstance() {
220241
try {
221242
if (options.has("logo")) {
222243
int id = getIdentifier(options.getString("logo"), "drawable");
223-
instance = instance.setLogo(id);
244+
// instance = instance.setLogo(id);
224245
}
225246
if (options.has("theme")) {
226247
int id = getIdentifier(options.getString("theme"), "style");
@@ -229,11 +250,15 @@ private AuthUI.SignInIntentBuilder buildCustomInstance() {
229250
int id = getIdentifier("FirebaseUILogonTheme", "style");
230251
instance = instance.setTheme(id);
231252
}
232-
if (options.has("tosUrl")) {
233-
instance = instance.setTosUrl(options.getString("tosUrl"));
234-
}
235-
if (options.has("privacyPolicyUrl")) {
236-
instance = instance.setPrivacyPolicyUrl(options.getString("privacyPolicyUrl"));
253+
if (options.has("tosUrl") && options.has("privacyPolicyUrl")) {
254+
instance = instance.setTosAndPrivacyPolicyUrls(options.getString("tosUrl"),options.getString("privacyPolicyUrl"));
255+
} else {
256+
if (options.has("tosUrl")) {
257+
instance = instance.setTosUrl(options.getString("tosUrl"));
258+
}
259+
if (options.has("privacyPolicyUrl")) {
260+
instance = instance.setPrivacyPolicyUrl(options.getString("privacyPolicyUrl"));
261+
}
237262
}
238263
if (options.has("smartLockEnabled")) {
239264
smartLockEnabled = options.getBoolean("smartLockEnabled");
@@ -255,14 +280,81 @@ private int getIdentifier(String name, String type) {
255280
cordova.getActivity().getApplicationContext().getPackageName());
256281
}
257282

283+
@Override
284+
public void onActivityResult(int requestCode, int resultCode, Intent data) {
285+
super.onActivityResult(requestCode, resultCode, data);
286+
287+
Log.d(TAG, "onActivityResult");
288+
Log.d(TAG, "onActivityResult:requestCode:" + requestCode);
289+
290+
if (requestCode == RC_SIGN_IN) {
291+
Log.d(TAG, "onActivityResult:resultCode:" + resultCode);
292+
293+
IdpResponse response = IdpResponse.fromResultIntent(data);
294+
295+
if (resultCode == RESULT_OK) {
296+
297+
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
298+
raiseEventForUser(user);
299+
} else {
300+
if (response == null) {
301+
signInAnonymous(firebaseAuth);
302+
raiseEvent(callbackContext, "signinaborted", null);
303+
return;
304+
}
305+
306+
if (response.getError().getErrorCode() == ErrorCodes.ANONYMOUS_UPGRADE_MERGE_CONFLICT) {
307+
handleAnonymousUpgradeMergeConflict(response);
308+
} else {
309+
310+
if (response.getError().getErrorCode() == ErrorCodes.NO_NETWORK) {
311+
raiseErrorEvent("signinfailure", response.getError().getErrorCode(), "No network so unable to sign in.");
312+
313+
return;
314+
}
315+
316+
Log.e(TAG, "Sign-in error: ", response.getError());
317+
raiseErrorEvent("signinfailure", response.getError().getErrorCode(), "There was a problem signing in.");
318+
}
319+
}
320+
}
321+
}
322+
323+
private void handleAnonymousUpgradeMergeConflict(IdpResponse response) {
324+
AuthCredential nonAnonymousCredential = response.getCredentialForLinking();
325+
326+
FirebaseAuth.getInstance().signInWithCredential(nonAnonymousCredential)
327+
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
328+
@Override
329+
public void onSuccess(AuthResult result) {
330+
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
331+
raiseEventForUser(user);
332+
}
333+
});
334+
}
335+
258336
private boolean signOut(CallbackContext callbackContext) {
259337

260338
Log.d(TAG, "signOut");
261339

262-
AuthUI.getInstance().signOut((FragmentActivity) cordova.getActivity());
263-
raiseEvent(callbackContext, "signoutsuccess", null);
340+
AuthUI.getInstance().signOut((FragmentActivity) cordova.getActivity()).addOnCompleteListener(new OnCompleteListener<Void>() {
341+
@Override
342+
public void onComplete(@NonNull Task<Void> task) {
343+
if (task.isSuccessful()) {
264344

265-
this.signInAnonymous(firebaseAuth);
345+
Log.d(TAG, "signOut: success");
346+
347+
raiseEvent(callbackContext, "signoutsuccess", null);
348+
signInAnonymous(firebaseAuth);
349+
}
350+
}
351+
}).addOnFailureListener(new OnFailureListener() {
352+
@Override
353+
public void onFailure(@NonNull Exception e) {
354+
Log.w(TAG, "signOut: failure", e);
355+
raiseErrorEvent("signoutfailure", 1, "There was a problem signing out.");
356+
}
357+
});
266358

267359
return true;
268360
}
@@ -365,25 +457,6 @@ private void raiseEventForUser(FirebaseUser user) {
365457

366458
}
367459

368-
@Override
369-
public void onActivityResult(int requestCode, int resultCode, Intent data) {
370-
super.onActivityResult(requestCode, resultCode, data);
371-
372-
Log.d(TAG, "onActivityResult");
373-
Log.d(TAG, "onActivityResult:requestCode:" + requestCode);
374-
375-
if (requestCode == RC_SIGN_IN) {
376-
Log.d(TAG, "onActivityResult:resultCode:" + resultCode);
377-
378-
if (resultCode == RESULT_OK) {
379-
380-
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
381-
raiseEventForUser(user);
382-
}
383-
}
384-
}
385-
386-
387460
private void raiseEvent(CallbackContext callbackContext, String type, Object data) {
388461

389462
Log.d(TAG, "raiseEvent: " + type);
@@ -419,22 +492,4 @@ public void onComplete(@NonNull Task<AuthResult> task) {
419492
raiseEvent(callbackContext, "signinfailure", data);
420493
}
421494
}
422-
423-
@Override
424-
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
425-
426-
Log.d(TAG, "onAuthStateChanged");
427-
428-
final FirebaseUser user = firebaseAuth.getCurrentUser();
429-
if (user != null) {
430-
Log.d(TAG, "onAuthStateChanged: signed in");
431-
432-
raiseEventForUser(user);
433-
434-
} else {
435-
Log.d(TAG, "onAuthStateChanged: signed out");
436-
437-
raiseEvent(callbackContext, "signoutsuccess", null);
438-
}
439-
}
440495
}

src/ios/FirebaseUIAuthPlugin.m

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ - (void)initialise:(CDVInvokedUrlCommand *)command {
3636
self.anonymous = true;
3737
}
3838

39-
[[FIRAuth auth] addAuthStateDidChangeListener:^(FIRAuth * _Nonnull auth, FIRUser * _Nullable user) {
40-
if (user != nil) {
41-
[self raiseEventForUser:user];
42-
} else {
43-
[self raiseEvent:@"signoutsuccess" withData:nil];
44-
}
45-
}];
46-
47-
[self signInAnonymous];
39+
// [[FIRAuth auth] addAuthStateDidChangeListener:^(FIRAuth * _Nonnull auth, FIRUser * _Nullable user) {
40+
// if (user != nil) {
41+
// [self raiseEventForUser:user];
42+
// } else {
43+
// [self raiseEvent:@"signoutsuccess" withData:nil];
44+
// }
45+
// }];
46+
47+
// [self signInAnonymous];
4848
}
4949
@catch (NSException *exception) {
5050
NSLog(@"Initialise error %@", [exception reason]);

www/browser/firebaseUIAuthPlugin.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ var loadCSS = function(url, loaded, implementationCode, location) {
3636
function FirebaseUIAuth(options, resolve) {
3737

3838
var self = this;
39-
var uiConfig;
40-
var uiElement;
41-
var anonymous;
4239

4340
var initialise = function() {
4441
if (firebase.apps.length === 0) {
@@ -50,7 +47,6 @@ function FirebaseUIAuth(options, resolve) {
5047
});
5148

5249
self.anonymous = options.anonymous ? true : false;
53-
self.signInAnonymously();
5450

5551
var providers = self.buildProviders(options.providers);
5652
self.buildUIConfig(options, providers);

www/firebaseUIAuthPlugin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ function FirebaseUIAuth(options) {
2424
return exec(dispatchEvent, null, PLUGIN_NAME, 'signIn', []);
2525
};
2626

27+
this.signInAnonymously = function() {
28+
return exec(dispatchEvent, null, PLUGIN_NAME, 'signInAnonymously', []);
29+
};
30+
2731
this.signOut = function() {
2832
return exec(dispatchEvent, null, PLUGIN_NAME, 'signOut', []);
2933
};

0 commit comments

Comments
 (0)